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 with variables, input/output streams, and basic arithmetic operations in C++.

You will use the `cin` object from the `iostream` library to capture user input, and `cout` to display the result. This exercise also reinforces the concept of variable declaration and usage, which is fundamental to any C++ program.

Key Concepts:
- Declaring and using variables
- Capturing user input with `cin`
- Performing arithmetic operations (addition)
- Displaying output using `cout`

Group

Introduction to C++

Objective

1. Declare two variables to store the numbers entered by the user.
2. Use `cout` to prompt the user for input.
3. Use `cin` to read the numbers from the user.
4. Calculate the sum of the two numbers.
5. Display the result using `cout`.

Performs a sum of two numbers entered by the user.

Example C++ Exercise

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

using namespace std; // Use the standard namespace

// Main function - starting point of the program
int main() {
    int num1, num2, sum; // Declare variables to store two numbers and their sum

    // Ask the user to enter the first number
    cout << "Enter the first number: ";
    cin >> num1; // Read the first number from user input

    // Ask the user to enter the second number
    cout << "Enter the second number: ";
    cin >> num2; // Read the second number from user input

    // Calculate the sum of the two numbers
    sum = num1 + num2;

    // Display the result to the user
    cout <<"The sum is: " << sum << endl;

    return 0; // Indicate that the program ended successfully
}

 Output

Enter the first number: 7
Enter the second number: 5
The sum is: 12

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++.

  • 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...

  • Check if a Number is Even or Odd in C++

    This exercise teaches you how to determine whether an integer number is even or odd using conditional statements in C++. It’s a fundamental concept that helps beginners understand ...

  • Currency Converter: Euros to US Dollars in C++

    This exercise is designed to help you understand how to work with floating-point numbers and user input/output in C++. You will build a basic currency converter that takes an amoun...

  • Calculate the Perimeter of a Rectangle in C++

    This exercise focuses on computing the perimeter of a rectangle using basic arithmetic operations in C++. It’s a great way for beginners to get hands-on practice with input/output,...