Evaluating expression [C Program]

This C program evaluates the expression

1 + 2!/3! + 3!/4! + 4!/5! + ... n!/(n+1)!

The program evaluates the expression for the value of n.

Notice in this program that, in the function part, we have skipped the initialization in the for loop. This is because we have initialized the variable earlier.


#include<stdio.h>
int facto(int f)
{
int fact = 1;
for(;f>0;f--)
{
fact=fact*f;
}
return fact;
}
int main()
{
int n,i;
float ans = 1,term;
printf("Enter the value of n: ");
scanf("%d",&n);
for(i=2;i <= n;i++)
{
term = (float)n/facto(n + 1);
ans = ans + term;
}
printf("The value is %f ",ans);
getch();
return(0);
}
view raw gistfile1.txt hosted with ❤ by GitHub

Comments