1. ## Do-while statements

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");

}```

2. You do have one thing you need to fix:
Code:
`euler.c:26: warning: format ‘%f’ expects type ‘double’, but argument 2 has type ‘int’`
As to the problem, I would guess you have your while condition backwards. For instance if input is 0.25; the first computed term is 1.0; 1.0 < 0.25 is false, so the loop stops.

3. 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 ; <-- that doesn't seem very much like factorial to me.
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",(double)no_of_terms);
system ("pause");

}```

4. Originally Posted by tabstop
You do have one thing you need to fix:
You obviously aren't counting the horrible random indentation then .