Q:

C program to calculate the sum of series 1 +1/x^1 + 1/x^2 + 1/x^3 ... + 1/x^n terms

0

C program to calculate the sum of series 1 +1/x^1 + 1/x^2 + 1/x^3 ... + 1/x^n terms

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to calculate sum of series 1 +1/x^1 + 1/x^2 + 1/x^3 ... + 1/x^n terms in C

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

int main()
{
	int x,n,i;
	float sum=0;
	
	printf("Enter total number of terms: ");
	scanf("%d",&n);
	
	printf("Enter the value of x: ");
	scanf("%d",&x);	
	
	for(i=1; i<=n; i++){
		sum += 1+(1/pow(x,i));
	}
	
	printf("Sum of the series: %f\n",sum);
	return 0;
}

Output

Enter total number of terms: 5
Enter the value of x: 2 
Sum of the series: 5.968750

Solution

Series is:
1+1/x^1 + 1/x^2+ 1/x^3 ... + 1/x^n

Here,
    x=2
    n=5

1+1/2^1 + 1+1/2^2 + 1+1/2^3 + 1+1/2^4 + 1+1/2^5
1+1/2 + 1+1/4 + + 1+1/8 + 1+1/16 + 1+1/32
1+0.5 + 1+0.25 + 1+0.125 + 1+0.0625 + 1+0.03125
= 5.968750

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now