In this tutorial, we are going to make a C Program to print the sum of N natural numbers with step by step full complete explanation.
C Program to print the sum of N natural numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int input, i, sum=0;
printf("Enter any number:");
scanf("%d", &input);
for(i=1;i<=input; i++)
sum=sum+i;
printf("sum of %d natural number: %d",input, sum);
getch();
}
Output
Explanation
Here, first of all, we include the required header files stdio.h and conio.h and then we declare the main function and then we declare the integer variables input, i and sum, and initialize sum to zero.
after that, we use the printf() and scanf() function to get the input by the user. and then using for loop we find the sum of N numbers and then print on the output screen.