I want to calculate sum of 1 to n using loop and this is my C code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
long sum = 0;
int n = atoi(argv[1]);
for(int i = 1; i<= n; i++)
sum += i;
printf("Sum of serial from 1 to %d = %ld\n", n, sum);
return 0;
}
when I run the program with small n, the result is correct but when I run with large number this is wrong. Here is the result when I run this:
./sum 100000
Sum of serial from 1 to 100000 = 705082704