C Program for Sum Of N Natural Numbers

Find the Sum of N Natural Numbers in C

Find the Sum of N Natural Numbers in C

Given an integer input num, the objective is to write a code to find the Sum of N Natural Numbers in C. To do so we iterate and add all the numbers until num.

Example:
Input : 5
Output : 1 + 2 + 3 + 4 + 5 = 15

Algorithm

The Algorithm for the above problem is as follows,

  • 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

Let’s try and understand it better using the Image given below.

C Program for Sum Of N Natural Numbers

#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

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Getting Started