Sum of the First N Natural Numbers in C++

This exercise introduces the concept of summation using a loop in C++. You will create a program that calculates the sum of the first N natural numbers, where N is provided by the user. This is a common task in mathematics and programming, and it's an ideal way to learn how loops and variables work together.

The program will prompt the user to enter a positive integer N. Then, using a loop structure, it will sum all the numbers from 1 to N and display the total. This technique is often used in statistical calculations, number theory, and performance testing for loops.

By completing this exercise, you will improve your understanding of iterative structures, conditional logic, and input validation in C++.

Group

Introduction to C++

Objective

1. Declare an integer variable to store the user's input (N) and another to store the sum.
2. Ask the user to enter a positive integer.
3. Use a loop to iterate from 1 to N and add each number to the sum.
4. Display the final result.

Sum the first N natural numbers.

Example C++ Exercise

 Copy C++ Code
#include <iostream> // Include the iostream library to handle input and output

using namespace std; // Use the standard namespace

// Main function - the starting point of the program
int main() {
    int N, sum = 0; // Declare an integer N for user input and sum initialized to 0

    // Prompt the user to enter a positive number
    cout << "Enter a positive integer: ";
    cin >> N; // Read user input and store it in variable N

    // Use a for loop to calculate the sum from 1 to N
    for (int i = 1; i <= N; ++i) {
        sum += i; // Add each number to the total sum
    }

    // Display the result of the summation
    cout << "The sum of the first " << N << " natural numbers is: " << sum << endl;

    return 0; // Return 0 to indicate that the program ended successfully
}

 Output

Enter a positive integer: 5
The sum of the first 5 natural numbers is: 15

Share this C++ Exercise


More C++ Programming Exercises of Introduction to C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.

  • Print the First 10 Fibonacci Numbers in C++

    This exercise helps you understand how to generate a sequence of numbers based on mathematical logic using iterative programming. Specifically, you'll write a program in C++ that p...

  • Hello World Program in C++

    The "Hello, World!" program is one of the simplest programs to write in any programming language. It serves as an introduction to the syntax and basic functionality of a programmin...

  • Sum of Two Numbers Entered by the User in C++

    This exercise introduces user input handling in C++ through a simple program that adds two numbers provided by the user. It's an ideal starting point for understanding how to work ...

  • Convert Celsius to Fahrenheit in C++

    This beginner-friendly exercise demonstrates how to perform a temperature conversion from Celsius to Fahrenheit using a basic arithmetic formula in C++. It's an excellent introduct...

  • Calculate the Area of a Circle in C++

    This exercise helps you practice using variables, mathematical operations, and user input/output in C++ by calculating the area of a circle. The formula to compute the area of a ci...

  • Multiplication Table Generator in C++

    This exercise focuses on generating the multiplication table of a number provided by the user. It introduces the use of loops in C++, specifically the `for` loop, to repeat a set o...