ADVERTISEMENT
ADVERTISEMENT

C program to find the sum of series x + x/2! + x/4! + ... + x/n!

In this C program, we are going to find the sum of series x + x/2! + x/4! + ... + x/n!, where value of x and n will be provided by the user.
Submitted by IncludeHelp, on March 18, 2018

Given the value of x and n and we have to find the sum of series x + x/2! + x/4! + ... + x/n! Using C program.

Program to find the sum of series x + x/2! + x/4! + ... + x/n! in C

/*  
C program to find sum of following series
* x+x/2!+x/4!+.....x/n!
*/

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

/* function : factorial, to find factorial of a given number*/
long int factorial(int n)
{
    int i;
    long int fact=1;
 
    if(n==1) return fact;
 
    for(i=n;i>=1;i--)
        fact= fact * i;
 
    return fact;
}

// main function
int main()
{
	int i,N,x;
	float sum;
	int count=1,cnt=0;
	
	// reade the value of x
	printf("\nEnter the value of x : ");
	scanf("%d",&x);
	
	/*read value of N*/
	printf("Enter total number of terms: ");
	scanf("%d",&N);
	
	/*set sum by 0*/
	sum=0.0f;
	
	/*calculate sum of the series*/
	for(i=1;i<=N;i++)
	{
		sum = sum + ( (float)(x) / (float)(factorial(count)));
		count = (count*2);
		//printf("\ncount : %d\n",count);	fflush(stdout);
	}
	
	/*print the sum*/
	printf("Sum of the series is: %f\n",sum);
	
	return 0;
}

Output

Enter the value of x : 2
Enter total number of terms: 3
Sum of the series is: 3.083333

C Sum of Series Programs »


Read more...

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.