Search code examples
cfor-loopmathsumfactorial

C program to find the sum of factorials


I was trying to compute the sum of series which involves factorials.

The series is: 1 + 1/2! + 1/3! + 1/4! + ...

btw 3! = 1 x 2 x 3

4! = 1 x 2 x 3 x 4

...so on, it continues like this for every number

This is the code I've written to run the C program:

#include <stdio.h>

void main() {

    /* Any problems with the variables? */
        
    int i, lim, fact = 1;
    float sum = 0.0, term;
    
    printf("Enter the limit for the sum of series: ");
    scanf("%d", &lim);
    
    /* Any problems in the loop? */
    
    for (i = 1; i <= lim; i++) {
        fact *= i;
        term = 1 / fact;
        sum += fact; 
    }

    printf ("%f is the sum of the series\n", sum);
}

Is there any syntax error here that I just can't see or something, or is it a problem of the compiler. If so, I'm using gcc on ubuntu. Nevertheless, I'm not getting the correct output.


Solution

  • There are some mistakes in your code.

    The first one, is very common when we start learning C

    1 / fact must be replaced by 1.0f / fact

    Why? Because in C, 1 / 2 is performed using integer arithmetic and returns 0. If you want to perform computation using floating numbers you must force at least one argument being a floating number, this can be done by 1.0/2, 1/2.0 or 1.0/2.0. The previous expressions will be evaluated using double floating number type (which is the C default type for floating number). If you want to use simple precision floating numbers, float, you can use 1.0f

    the second one is certainly an inadvertent error:

    sum += fact; must be replace by sum += term;


    Here is a "working" code

    #include <stdio.h>
    
    void
    main()
    {
      /*Any problems with the variables ?*/
    
      int i, lim, fact = 1;
      float sum = 0.0, term;
    
      printf("Enter the limit for the sum of series: ");
      scanf("%d", &lim);
    
      /*Any problems in the loop ? */
    
      for (i = 1; i <= lim; i++)
      {
        fact *= i;
        term = 1. / fact; /* instead of 1/fact */
        sum += term;      /* instead of sum += fact */
      }
    
      printf("%f is the sum of the series\n", sum);
    }
    

    Enter the limit for the sum of series: 5

    1.716667 is the sum of the series


    There is a more subtle error. By instance, if you want lim = 50,

    Enter the limit for the sum of series: 50

    inf is the sum of the series

    The reason is that 50! is way too big to be stored in an int.

    The solution is to compute directly 1/i! using floating number (and not i! using int then 1/i!)

    A modified program, that also uses double for an higher precision is :

    #include <stdio.h>
    
    void
    main()
    {
      /*Any problems with the variables ?*/
    
      int lim;
      double sum = 0.0, term = 1.0;
    
      printf("Enter the limit for the sum of series: ");
      scanf("%d", &lim);
    
      /*Any problems in the loop ? */
    
      for (int i = 1; i <= lim; i++)
      {
        term = term / i;
        sum += term;
      }
    
      printf("%f is the sum of the series\n", sum);
    }
    

    that now works, even when lim=50


    Do not be discouraged by these mistakes. These are mistakes we all made when we learned C !


    Expected result:

    exp(1) = exp(0) + sum_{i=1}^\infty 1/i!

    thus your expected result is exp(1)-exp(0) = 1.718281828459045....