Sum of first N natural numbers
C program to find the sum of first N natural numbers, N must be taken by the user.
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, sum = 0;
printf("Enter the value of N\t:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum = sum + i;
}
printf("Sum of first %d natural numbers is %d",n, sum);
getch();
}
Output
Enter the value of N :6
Sum of first 6 natural numbers is 21