1

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.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
yoyoyoyo
  • 21
  • 1
  • 6
  • If there is a syntax error, your compiler will immediately tell you because it cannot compile your code in that case. But you have other errors: `void main` is wrong. `main` is defined to have return type `int` in hosted environments. And you do not check the return value of `scanf`. – Gerhardh Apr 02 '22 at 06:31
  • Why are you adding `sum += fact`? That is not what your formula tells you to do. – Gerhardh Apr 02 '22 at 06:32
  • 2
    term is always 0 because you're dividing integers (you probably want 1.0 / fact) and you're not adding the right thing to sum. Also fact will quickly overflow because factorials grow quickly. – Paul Hankin Apr 02 '22 at 06:33
  • 1
    `sum += fact;` should be `sum += term;`? – kaylum Apr 02 '22 at 06:33
  • In addition to `sum += term`, you need to correct `term = 1 / fact` to `term = 1.0 / fact`. – tshiono Apr 02 '22 at 06:37
  • 1
    `float` **no, no, no** always prefer `double` when dealing with floating-point values (except when you have a very strong reason to do otherwise) – pmg Apr 02 '22 at 08:18

3 Answers3

2

At least these problems:

1 / fact is int division with an int quotient.

Try 1.0f / fact for a float division and float quotient.

sum += fact; --> sum += term; is code should sum the terms.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
2

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....

Picaud Vincent
  • 10,518
  • 5
  • 31
  • 70
-2

Try it with int main() once and let me know whether it is working or not