Add Two Numbers In C++ Program - C++ Programming Tutorial
C++ Course / C++ Programs / Add Two Numbers In C++ Program

Add Two Numbers In C++ Program

BLUF: Mastering Add Two Numbers In C++ Program is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Add Two Numbers In C++ Program

C++ is renowned for its efficiency. Learn how Add Two Numbers In C++ Program enables low-level control and high-performance computing in the tutorial below.

In C++, you can perform the addition of two numerical values using arithmetic operators. The addition operation is carried out using the plus symbol (+). To sum two numbers in C++, you start by defining variables to store the values and then apply the plus operator to compute their sum.

C++ Code:

Example

#include <iostream>
using namespace std;
int main() {
    int num1, num2, sum;
    cout << "Enter the first number: ";
    cin >> num1;
    cout << "Enter the second number: ";
    cin >> num2;
    sum = num1 + num2;
    cout << "The sum of the two numbers is: " << sum;
    return 0;
}

Output

Output

Enter the first number: 4
Enter the second number: 7
The sum of the two numbers is: 11

Explanation:

The provided C++ script is designed to perform addition of two numbers entered by the user and display the result on the console. It commences by including the "iostream" library that facilitates input and output operations. The script initiates by displaying a message requesting the user to input the initial number, awaits user input, and then prompts for the second number. Subsequently, it defines three integer variables: "num1", "num2", and "sum" to hold the user-input numbers and the sum. The script utilizes the "cout" statement to ask the user for the first number and the "cin" statement to receive the input value, which is then stored in the "num1" variable. Following this, the user is prompted for the second number using another "cout" statement, and the input value is stored in the "num2" variable after being inputted via the "cin" statement.

Once both numbers are provided as input, the software combines them by utilizing the "+" symbol and saves the outcome in the "sum" variable. Subsequently, the software displays the sum of the two numbers through the "cout" command, along with a notification specifying that the displayed result represents the total of the two numbers. Following the display of the output, the software concludes with the "return 0" statement, signifying the successful execution of the program.

C++ Code:

Example

#include <iostream>
using namespace std;
int main() {
    long long num1, num2, sum;
    cout << "Enter the first number: ";
    cin >> num1;
    cout << "Enter the second number: ";
    cin >> num2;
    sum = num1 + num2;
    cout << "The sum of the two numbers is: " << sum;
    return 0;
}

Output

Output

Enter the first number: 123456789
Enter the second number: 987654321
The sum of the two numbers is: 1111111110

Explanation:

The provided code demonstrates a C++ script that requests the user to enter two significant numbers, performs addition on them, and then displays the resulting sum on the screen. Initially, it imports the "iostream" library to enable input and output functionalities. Subsequently, it defines three variables of type long long integer: "num1", "num2", and "sum". These variables are designated to hold the two input values and the total sum calculated from them.

The software then requests the user to input the initial number through the "cout" command, and remains idle for the user to provide a value using the "cin" command. The input value is then saved in the "num1" variable. Following this, the application prompts the user to input the second number using a separate "cout" command, and again waits for the user to input a value using another "cin" command. The entered value is then stored in the "num2" variable.

Upon entering the two numerical values, the software combines them by utilizing the addition operator and then saves the outcome within the "sum" variable. Subsequently, the software displays the sum of the two numbers employing the "cout" command, accompanied by a notification specifying that the displayed result represents the total of the two numbers. In this program, the utilization of the long long data type enables the handling of significantly larger numbers compared to the int data type utilized in the earlier illustration, rendering it appropriate for summing very extensive values. The inclusion of the "return 0" statement towards the conclusion of the program serves as an indication that the software has executed without any issues.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience