/* Write a program and calculate the value of E according to the following formula. Two calculation methods are required: 1) for loop, calculation of the first 50 items 2) while loop until the value of the last item is less than 10-4e = 1 + 1/1! + 1/2! + 1/3! + 1/4! + 1/5! +... + 1/n! */# Include <stdio. h> // ============================================ ==========================/// use for to evaluate the value of e double () {double sum = 1, temp = 1; int I; for (I = 1; I <50; I ++) {temp/= I; sum + = temp ;} return sum ;} // ================================================ ====================/// use the while loop to evaluate the value of e double while () {double sum = 1; // The first item is set to 1 double temp = 1; int I = 1; while (temp> = 1e-4) {// [E = 1 + 1/1! + 1/2! + 1/3! + 1/4! + 1/5! +... + 1/n !] Temp = temp/I; // The second item is 1/1, that is, 1/1 !; Item 3: 1/2, that is, 1/2 !; Item 4 (1/2)/3, I .e. 1/3 !... Sum = sum + temp; I ++;} return sum; // return sum} // main function int main () {double A = (); double B = while (); printf ("Use the for loop to find the sum of the first 50 items of E is % lf \ n", ); printf ("using the while loop to obtain e and % lf \ n", B); Return 0 ;}
Use the for and while loops to calculate the value of E [E = 1 + 1/1! + 1/2! + 1/3! + 1/4! + 1/5! +... + 1/N!]