Pages

This blog is under construction

Sunday, January 6, 2019

Write a program to compute the value of Euler’s number that is used as the base of natural logarithms.

Use the following formula.
e= 1+ 1/1! +1 /2! + 1/3+……………. 1/n!

C code:

#include<stdio.h>
int fact(int);
void main()
{
int i,n;
float sum=0;
printf("Enter No.");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
sum=sum+((float)1/fact(i));
printf("1/!%d+",i);
}
printf("\nThe value of e is : %f",sum);
}

int fact(int n)
{ int f=1,i;
  if(n==0)
     return 1;
 for(i=1;i<=n;i++)
 {      f=f*i;
 }
 return f;
}

Output:

Enter No.10                                                                                                                    
1/!0+1/!1+1/!2+1/!3+1/!4+1/!5+1/!6+1/!7+1/!8+1/!9+1/!10+                                                                       
The value of e is : 2.718282

No comments:

Post a Comment