0

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;
}
mmixLinus
  • 1,646
  • 2
  • 11
  • 16
Erik13
  • 9
  • 1

2 Answers2

1

Please note that math operations between integers in C yield integers.

num1 = 1 / i;

1 divided by anything other than 0 (obviously problematic) or 1 will result in 0.

You likely want to cast i to a float or double in this operation.

num = 1 / (float)i;

if num1 and num2 are both 0, then eucl = eucl + num1 + num2; is really just eucl = eucl;.

Chris
  • 26,361
  • 5
  • 21
  • 42
1

You are attempting to calculate an approximation to Euler's number (2.718...). (This has nothing to do with Euclid.) The formula you want to use is:

e = sum(n=0->inf) {1/n!}

There are some errors mentioned by previous comments. However, your faculty calculation is wrong. One can calculate faculty using recursion:

int calculatingFaculty(int n) {
    return n < 2 ? 1 : n * faculty(n-1);
}

You should probably change your termination condition so that it loops as long as the difference between two terms is bigger than the entered value, because I assume the difference is going to get smaller and smaller if the sum is going to have a chance of converging..

mmixLinus
  • 1,646
  • 2
  • 11
  • 16