×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

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

Here, we are going to implement a c program that will find the sum of series 1 +1/x^1 + 1/x^2 + 1/x^3 ... + 1/x^n terms. Submitted by IncludeHelp, on March 04, 2018

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

Explanation

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

C Sum of Series Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.