Program to find sum of the series 1 + ( 1 + x ) + ( 1 + x + x^ 2 ) +...n terms.

Write a program to sum of the series  1 + ( 1 + x ) + ( 1 + x + x^ 2 ) +...n terms.

Formula:

Sn​=1−xn​−(x−1)2x(xn−1)​

Example:

Input:  n=4, x=2
Output: Sum of series is -34.000000

Approach

C

#include <stdio.h>
#include <math.h>

int main()
{
    int n, x;
    printf("Enter value of n: ");
    scanf("%d", &n);

    printf("Enter value of x: ");
    scanf("%d", &x);

    float ans = n / (1 - x) - (x * (pow(x, n) - 1)) / (pow(x - 1, 2));

    printf("Sum of series is %f", ans);

    return 0;
}


No comments:

Post a Comment