C program to calculate sum of first N natural numbers.

This program will read the value of N and calculate sum from 1 to N, first N natural numbers means numbers from 1 to N and N will be read through user.

/*C program to calculate sum of first N natural numbers.*/
#include <stdio.h>
int main()
{
int n, i;
int sum;
printf("Enter the value of N: ");
scanf("%d", &n);
sum = 0;
for (i = 1; i <= n; i++)
sum += i;
printf("Sum is: %d\n", sum);
return 0;
}

Output

RUN 1:
Enter the value of N: 100
Sum is: 5050
RUN 2:
Enter the value of N: 10
Sum is: 55
RUn 3:
Enter the value of N: 3
Sum is: 6

## Sum of first N natural numbers using C program

need an explanation for this answer? contact us directly to get an explanation for this answerOutput