I am very new to C programming and am trying to create a program as part of an assignment.
The idea is to make a program to evaluate a summation series, E.
E=1+ 1/1! +1/2! + 1/3! +1/4! ........
The program should evaluate the series up to the point where one of the terms in it is less than a user specified limit. It then displays the series summation on the screen and tells the user the number of terms that it evaluated to calculate the series.
I have tried using a do-while statement. At the moment, any number entered by the user that is less than one gives a result of 2.0000 which is not right and a number entered greater than 1 causes the do loop to run for ever. Surely if the number entered is greater than one it should execute the statements in the loop once and then break out of the loop as none of the terms can possibly be greater than 1.
I am not sure if I have the order of expressions right in the loop or whether I have left out an expression that should be in the loop. Also, I am not sure whether I have entered the while logical expression correctly: - Does C interpret the word "input" that I have put in the logical expression as the input variable defined at the beginning of the code or just as an undefined word.
The code is below.
Thank you!
Paul
Code:
#include <stdio.h>
#include <math.h>
main()
{
int n, no_of_terms;
float E, term, input;
printf("Input minimum value of a term: ");
scanf("%f", &input);
n=1;
E=1.0;
do
{
term=1.0/n ;
E = E+term ;
n = n + 1 ;
no_of_terms = n + 1;
}
while ( term < input);
printf("E=%f \n" , E);
printf("Number of terms in evaluation: %f \n",no_of_terms);
system ("pause");
}