calculate the sum of n natural numbers using c program




To find the sum of N integers, we have to write a c program which using a for loop from 1 to N. In this C program we get the value of N from the scan f statement.

Now we write a for loop to find the sum of N numbers starting from 1. Initialize sum = 0 in the beginning. total Sum is added to the variable 'sum' in each iteration of the for loop, then the output is displayed at the end of the for loop.

===================================================================
#include <stdio.h>
 
#include <conio.h>
 
void main()
 
{
 
   	int i, N, sum = 0;
 
	clrscr();
 
  	 printf("Enter an integer number\n");                /* prints enter the number and gets the value from user */
 
	 scanf ("%d", &N);
 
   	for (i=1; i <= N; i++)                                        /* beginning of for loop */
 
	{
 
      		sum = sum + i;
 
	}
 
 
	printf ("Sum of first %d natural numbers = %d\n", N, sum);    /* prints the sum of the numbers */
 
}
==================================================================
OUTPUT of C program sum of n natural numbers
==================================================================
RUN1

Enter an integer number
10
Sum of first 10 natural numbers = 55


RUN2

Enter an integer number
50
Sum of first 50 natural numbers = 1275