Write a program to print the sum of first n natural numbers

Posted in

Write a program to print the sum of first n natural numbers
vinaykhatri

Vinay Khatri
Last updated on December 22, 2024

    Problem Statment

    We need to write a program, that prints the first n natural number and their sum. Basically, we need to write a program that prints the sum of the following series 1+2+3+4+5+...+n . Here n represent the last number of the series.

    For example

    Input

    n=4

    Output

    10

    Explanation

    the value of n is 4, so:
    =1+2+3+4
    =10

    Approach 1 (Brute Force)

    To compute the sum of the first n natural number problem, we will first use the brute force approach, in which we will take the help of for loop from range 1 to n and add all the numbers in the total sum.

    C program to print the sum of first n natural numbers

    #include <stdio.h>
    int main()
    {	
    	int sum =0, i, n;
    	printf("Here we will calculate the sum of series 1+2+3+4+5+..n  ...  \n");
    	
    	//input the value of n
    	printf("Enter the value of n: ");
    	scanf("%d", &n);
    	
    	//compute the sum
    	
    	for(i=1;i<=n; i++)
    	{		
    		//compute the sum		
    		sum += i;
    	}
    	
    	printf("The sum of the series is: %d",sum);
    
    	return 0;
    }

    Output:

    Here we will calculate the sum of series 1+2+3+4+5+..n ...á
    Enter the value of n: 15
    The sum of the series is: 120

    C++ Program to print the sum of first n natural numbers

    #include<iostream>
    using namespace std;
    int main()
    {
    	int sum=0,n; 
    	cout<<"Here we will calculate the sum of series 1+2+3+4+5+...n \n";
    	
    	//input the value of n
    	cout<<"Enter the value of n: "; cin>>n;
    	
    	for(int i=1;i<=n; i++)
    	{
    		//compute the sum		
    		sum += i;
    	}
    	
    	cout<<"The sum of the series is: "<<sum;
    	return 0; 
    }

    Output

    Here we will calculate the sum of series 1+2+3+4+5+...n
    Enter the value of n: 4
    The sum of the series is: 10

    Python program to print the sum of first n natural numbers

    print("Here we will calculate the sum of series 1+2+3+4+5+...n")
    
    # initialize the value of sum
    sum_=0
    
    # input the value of n
    n = int(input("Enter the value of n: "))
    
    for i in range(1, n+1):
        # compute the sum
        sum_+=i
    
    print(f"The sum of the series is: {sum_}")

    Output:

    Here we will calculate the sum of series 1+2+3+4+5+...n
    Enter the value of n: 15
    The sum of the series is: 120

    Complexity analysis

    Time Complexity: O(N) Space Complexity: O(1)

    Approach 2 (Using Formula)

    The time complexity of the above program is O(N), where N is the last number of the series. But in mathematics, we have a dedicated formula that can compute the sum of first N digit natural numbers.

    Sum of first N natural numbers = n*(n+1)/2

    C Program to print the sum of first n natural numbers

    #include <stdio.h>
    
    int main()
    {	
    	int sum , i, n;
    	printf("Here we will calculate the sum of series 1+2+3+4+5+..n  ...  \n");
    	
    	//input the value of n
    	printf("Enter the value of n: ");
    	scanf("%d", &n);
    	
    	//compute the sum
    	sum = n*(n+1)/2;
    	
    	printf("The sum of the series is: %d",sum);
    
    	return 0;
    }

    Output

    Here we will calculate the sum of series 1+2+3+4+5+...n
    Enter the value of n: 4
    The sum of the series is: 10

    C++ program to print the sum of first n natural numbers

    #include<iostream>
    using namespace std;
    int main()
    {
    	int sum=0,n; 
    	cout<<"Here we will calculate the sum of series 1+2+3+4+5+...n \n";
    	
    	//input the value of n
    	cout<<"Enter the value of n: "; cin>>n;
    	
    	//compute the sum
    	sum = n*(n+1)/2;
    	
    	cout<<"The sum of the series is: "<<sum;
    	return 0; 
    }

    Output

    Here we will calculate the sum of series 1+2+3+4+5+...n
    Enter the value of n: 5
    The sum of the series is: 15

    Python Program to print the sum of first n natural numbers

    print("Here we will calculate the sum of series 1+2+3+4+5+...n")
    
    # input the value of n
    n = int(input("Enter the value of n: "))
    
    # compute the sum
    sum_ = n*(n+1)//2;
    
    print(f"The sum of the series is: {sum_}")

    Output

    Here we will calculate the sum of series 1+2+3+4+5+...n
    Enter the value of n: 5
    The sum of the series is: 15

    Complexity analysis

    Time Complexity: O(1)

    Space Complexity: O(1)

    Wrapping Up!

    In this tutorial, we learned how to write a program in C, C++ and python to find out the sum of first n natural numbers. To write the program we follow two different approaches. In the first approach, we use the brute force technique which results in higher time complexity.

    But in the second approach, we use the mathematical formula that makes the program more efficient with constant time complexity. If you ever encounter this program in your programming interviews, there use the second approach, because it is more efficient and optimized, and it will make your code faster.

    People are also reading:

    Leave a Comment on this Post

    0 Comments