C Program to calculate Sum of Series

Calculating Sum of Series using C Programming Language


This C Program will find the sum of series 1/1! + 2/2! + 3/3! + 4/4! so on. Our aim is to calculate the series output using C coding. In this program I created a function to calculate the factorial of the given in order to find 1!, 2!,3! and so on, and then I sum the 1/1!,2/2! and so on to get the result. Just go through the below C source code.

//SumOfSeries.c

#include <stdio.h>
#include <conio.h>

long int factorial(int n);//factorial function
void main(){
int n,i;
float s,r;
char c;
clrscr();
repeat : printf("You have this series:-
1/1! + 2/2! + 3/3! + 4/4! .");
printf("
To which term you want its sum?
");
scanf("%d",&n);
s=0;
for (i=1;i<=n;i++){
s=s+((float)i/(float)factorial(i)); }
printf(" The sum of %d terms is %f",n,s);
fflush(stdin);
printf ("
Do you want to continue?(y/n):- ");
scanf("%c",&c);
if (c=='y')
goto repeat;
getch();
}
long int factorial(int n){//factorial using recursive function
if (n<=1)
return(1);
else
n=n*factorial(n-1);
return(n);
}


Related Topic Fibonacci Program in C Language
How to Calculate Day Difference between Date
Java Code to calculate Time Date Difference
Java to Calculate Standard Deviation
PHP to Calculate Age from Date

nScraps.com © 2011   Privacy Policy  Terms of Service  Feedback