C Program for Sum Of N Natural Numbers

c program of sum of n natural number

Find Sum of N Natural Numbers

In this article we will see a C Program to find Sum Of N Natural Numbers. In the C programming language, the user is allowed to insert any integer value. With the help of For loop, this C program can calculate the sum of N natural numbers. Within this program, first printf statement will request the user to insert a number or value then the scanf statement will allocate the user inserted value to integer variable. The sum is calculated in the For loop. 

To perform the arithmetic operation of addition of n numbers we use this conditions 
Example –
Enter Number  3
N natural numbers 1,2,3,4,5,6,7,8…….
Where first 3 number is 1,2,3
Then we will return  sum of number = 6

Algorithm

  • Step 1. Start
  • Step 2. Enter a number (N).
  • Step 3. Use “For loop” to iterate  upto the user inserted value. 
  • Step 4. The “For loop” will calculate the sum of the user inserted value. 
  • Step 5. Stop
Sum of n natural number

C code

#include<stdio.h>  
int main() 
{ 
    //for initialize variable
    int Number, i, Sum = 0;
    //to take user input
    printf ("\n Kindly Insert an Integer Variable\n");
    scanf ("%d", &Number);

    //use for loop for these condition
    for(i = 1; i <= Number; i++)
    {
         Sum = Sum + i;
    }

    //display
    printf ("Sum of Natural Numbers = %d", Sum);

    return 0;
}

Output

Kindly Insert an Integer Variable
3
Sum of Natural Numbers = 6