I am trying to calculate 1 + 1 * 2 + 1 * 2 * 3 + 1 * 2 * 3 * 4 + ... + 1 * 2 * ... * n
where n
is the user input.
It works for values of n
up to 12. I want to calculate the sum for n = 13
, n = 14
and n = 15
. How do I do that in C89? As I know, I can use unsigned long long int
only in C99 or C11.
- Input 13, result 2455009817, expected 6749977113
- Input 14, result 3733955097, expected 93928268313
- Input 15, result 1443297817, expected 1401602636313
My code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
unsigned long int n;
unsigned long int P = 1;
int i;
unsigned long int sum = 0;
scanf("%lu", &n);
for(i = 1; i <= n; i++)
{
P *= i;
sum += P;
}
printf("%lu", sum);
return 0;
}
printf("%d\n", num);
withprintf("%d\n", 1);
. – Pediculosis