C++ Program to Calculate the Sum of Natural Numbers

In this post, we will learn how to calculate the sum of natural numbers using C++ Programming language.

This program asks the user to enter a maximum natural number upto which the user wants the sum. Then, it calculate the sum of natural numbers using the following approaches:

  1. Using For Loop
  2. Using While Loop
  3. Using Do While Loop

So, without further ado, let’s begin this tutorial.

C++ Program to Calculate the Sum of Natural Numbers

// C++ Program to Calculate the Sum of Natural Numbers
#include <iostream>
using namespace std;

int main(){
    int num, sum = 0;
    
    // Asking for input
    cout << "Enter the maximum number: ";
    cin >> num;
    
    // Calculating sum using for loop
    for (int i =1; i <= num; i++){
        sum = sum + i;
    }
    
    // Displaying output
    cout << "Sum of natural numbers: " << sum << endl;
    return 0;
}

Output 

Enter the maximum number: 25
Sum of natural numbers: 325

How Does This Program Work ?

    int num, sum = 0;

In this program, we have declared two integer data type variables named num and sum, where the sum variable has assigned a value of 0.

    // Asking for input
    cout << "Enter the maximum number: ";
    cin >> num;

Now, the user is asked to enter a maximum number upto which the user wants the sum. This value gets stored in the num named variable.

    // Calculating sum using for loop
    for (int i =1; i <= num; i++){
        sum = sum + i;
    }

We initiate the for loop with the i = 1 and the value of i keeps incrementing by 1 until the value of i is less than or equal to num.

For every iteration, the sum variable becomes sum = sum + 1.

Suppose, the user enters the maximum number 3.

  • Then, After the 1st iteration sum = sum + i = 0 + 1 = 1.
  • After the 2nd iteration sum = sum + i = 1 + 2 = 3.
  • And after the 3rd iteration, sum = sum + i = 3 + 3 = 6.

After that i > num, so the loop terminates and we get sum = 6.

    // Displaying output
    cout << "Sum of natural numbers: " << sum << endl;

The sum calculated in the previous step is displayed on the screen using the cout statement.

C++ Program to Calculate the Sum of Natural Numbers Using While Loop

// C++ Program to Calculate the Sum of Natural Numbers Using While Loop
#include <iostream>
using namespace std;

int main(){
    int num, sum = 0;
    
    // Asking for input
    cout << "Enter the maximum number: ";
    cin >> num;
    
    int i = 1;
    while (i <= num){
        sum = sum + i;
        i++;
    }
    
    // Displaying output
    cout << "Sum of natural numbers: " << sum << endl;
    return 0;
}

Output

Enter the maximum number: 3
Sum of natural numbers: 6

C++ Program to Calculate the Sum of Natural Numbers Using Do While Loop

// C++ Program to Calculate the Sum of Natural Numbers Using Do While Loop
#include <iostream>
using namespace std;

int main(){
    int n, i = 1, sum = 0;
    
    // Asking for input
    cout << "Enter the maximum number: ";
    cin >> n;
    
    // Calculating the sum using do-while loop
    do {
        sum = sum + i;
        i++;
    }
    while (i <= n);
    
    // Displaying output
    cout << "Sum of natural numbers: " << sum << endl;
    return 0;
}

Output

Enter the maximum number: 10
Sum of natural numbers: 55

Conclusion

I hope after going through this post, you understand how to calculate the sum of natural numbers using C++ Programming language.

If you have any doubt regarding the program, then contact us in the comment section. We will be delighted to assist you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *