C Program to Find the Sum of First N Natural Numbers

Sum of First N Natural Numbers in C

In this article, we will see C Program to Find the Sum of First N Natural Numbers. A Natural number is the same as a Counting number. They are used to Count the number of real physical objects. Natural numbers start from 1 and go on infinite.

The positive numbers 1,2,3 ... are known as natural numbers.


Formula for Sum of First n natural numbers is : n(n+1)/2.
If you want to add first 5 Natural number 
then we find the Sum of 1+2+3+4+5 =15.
sum of first n natural number in c

Methods for C Program to Find the Sum of First N Natural Numbers

  • Method 1: Iterative way (Using for loop)
  • Method 2: Iterative way (Using while loop)
  • Method 3: Direct Formula
  • Method 4: Recursive Approach

Method 1: Using For Loop

For an input n

  • Create variable sum = 0
  • Run a for loop in iteration (I) from 1 -> n
    • Each time adding I to sum
  • Print the sum value after the loop terminates

Code

Run
#include <stdio.h>

int main()
{
    int n; 
    scanf("%d",&n);
    
    int sum = 0;
    
    for(int i=1;i<=n;i++) 
        // is same as writing sum = sum + i
        sum += i;
        
    printf("Sum is %d",sum);
    
    return 0;
}

// Time complexity : O(n)
// Space complexity : O(1)
 

Output

5
Sum is 15

Method 2: Using While Loop

Algorithm (in simple words):

  1. Start
  2. Take a number N from the user.
  3. Initialize two variables:
    • i = 1 (to start counting from 1)
    • sum = 0 (to store the final sum)

      4. Use a while loop until i <= N

    • Add i to sum
    • Increase i by 1

      5. When the loop ends, print the value of sum.

      6. End

Code:

Run

#include<stdio.h>

int main() {
    int N, i = 1, sum = 0;

    // Taking input from user
    printf("Enter a positive integer: ");
    scanf("%d", &N);

    // Loop to calculate the sum
    while(i <= N) {
        sum = sum + i;
        i++;
    }

    // Displaying the result
    printf("Sum of first %d natural numbers is: %d\n", N, sum);

    return 0;
}

Output

Enter a positive integer: 10
Sum of first 10 natural numbers is: 55

Prime Course Trailer

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

Method 3: Direct Formulae

For an input n

  • Create variable sum = 0
  • Use formula sum =  n(n+1)/2
  • Print the sum value

Code

Run
#include <stdio.h>

int main()
{
    int n; 
    scanf("%d",&n);
    
    int sum = n*(n+1)/2;

    printf("The sum is %d",sum);
    
    return 0;
}
// Time complexity : O(1)
// Space complexity : O(1)
 

Output

6
Sum is 21

Method 4: Recursive Approach

For an input n

  • Create variable sum = 0
  • Call function getSum(sum, n)
    • Base Case, n == 0 return sum
    • Others return n + getSum(sum, n-1)
  • Print the sum value in main

Code

Run
#include <stdio.h>

int getSum(int sum,int n)
{
    if(n==0) 
        return sum;
        
    return n+getSum(sum,n-1);
}

int main()
{
    int n, sum = 0; 
    scanf("%d",&n);

    printf("%d",getSum(sum, n));
    
    return 0;
}
// Time complexity : O(n)
// Space complexity : O(1)
// Auxilary space complexity : O(N)
// Due to function call stack
 

Output

5
Sum is 15

Getting Started

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