Write a program that reads a number N and approximates the value of e by the following partial sum: \(\sum_{n=0}^{N}\frac{1}{n!}=2+\frac{1}{2}+\frac{1}{3!}+ \cdots + \frac{1}{N!}\)
Difficulty level
This exercise is mostly suitable for students
#include<stdio.h>
#include<conio.h>
void main() {
int N, i;
double u,s;
do{
printf("Enter N: ");
scanf("%d",&N);
}while(N<=0);
s=u=1;
for(i=1;i<=N;i++)
{
u=u/i;
s=s+u;
}
printf("e = %lf\n",s);
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Statically implemented Binary Tree