This C Program should calculate Euler's number up to a certain point. it will calculate as long as the difference of 1/(n-1)! - 1/n! is smaller than the number the user entered.
The problem : no matter what number I enter it says that the Euler's number is 2.000000.
I think it is because it leaves the do while loop after the first time, I think that is because 1 - 1 equals 0 and therefore it is smaller than the number the user entered, I have already tried to stop this by adding the condition eul == 2, but when I run the program with this condition it does not work at all.
Here is my program:
#include <stdio.h>
float scanningTheDifference();
float calculatingTheEuclidianNumber(float d);
int calculatingFaculty(int i, int z);
void main () {
char answer;
float d;
double eul;
printf ("calculating the euclidian number\n");
do {
d = scanningTheDifference();
eucl = calculatingTheEuclidianNumber(d);
printf ("\n the euclidian number is: %lf", eucl);
do {
printf("\n \n do you want to repeat? (j/n)");
fflush(stdin);
scanf("%c", &answer);
} while (answer != 'y' && answer != 'n');
} while (answer == 'y');
}
float scanningTheDifference() {
float d; //difference
do {
printf("\n please enter the difference of two numbers, it has to be positive: ");
scanf("%f", &d);
} while (d < 0);
return d;
}
float calculatingTheEuclidianNumber(float d) {
double num1, num2;
double eucl = 0.0;
int i = 1;
int z = 1;
do {
num1 = 1 / i;
i = calculatingFaculty(i, z);
z++;
num2 = 1 / i;
i = calculatingFaculty(i, z);
z++;
eucl = eucl + num1 + num2;
} while (num1 - num2 < d);
return eucl;
}
int calculatingFaculty(int i, int z) {
int res;
res = i * z;
return res;
}