Write a program that 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!}\)
You should continuously add only the terms greater than \(10^{-10}\).
Difficulty level
This exercise is mostly suitable for students
#include<stdio.h>
#include<conio.h>
void main() {
int i;
double u,s;
s=u=1;
for(i=1;u>1e-10;i++)
{
u=u/i;
s=s+u;
}
printf("e = %lg, #terms = %d\n",s,i+1);
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Maximum sum in sliding window