Javatpoint Logo
Javatpoint Logo

Add two Numbers in C++ program

In C++, addition of two numbers can be performed using arithmetic operators. The arithmetic operator used for addition is the plus sign (+). To add two numbers, you first declare variables to hold the numbers and then use the plus sign to add them together.

C++ Code:

Output

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

Explanation:

The above code is a C++ program that adds two numbers inputted by the user and outputs the sum of the two numbers to the console. The program starts by including the "iostream" library, which provides input/output capabilities. As you can see, the program outputs a message prompting the user to enter the first number, waits for the user to input a value, and then outputs a message prompting the user to enter the second number. It then declares three integer variables: "num1", "num2", and "sum". These variables will be used to store the two numbers inputted by the user and the sum of those two numbers. The program prompts the user to enter the first number using the "cout" statement, and waits for the user to input a value using the "cin" statement. The value entered by the user is then stored in the "num1" variable. The program then prompts the user to enter the second number using another "cout" statement, and waits for the user to input a value using another "cin" statement. The value entered by the user is then stored in the "num2" variable.

After the two numbers are inputted, the program adds them together using the "+" operator and stores the result in the "sum" variable. Finally, the program outputs the sum of the two numbers using the "cout" statement, along with a message indicating that the output is the sum of the two numbers. Once the output is displayed, the program ends with the "return 0" statement, which indicates that the program has executed successfully.

C++ Code:

Output

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

Explanation:

The above code is a C++ program that prompts the user to input two large numbers, adds the two numbers together, and outputs the sum to the console. The program begins by including the "iostream" library, which provides input/output capabilities. It then declares three long long integer variables: "num1", "num2", and "sum". These variables will be used to store the two inputted numbers and the sum of those two numbers.

The program then prompts the user to enter the first number using the "cout" statement, and waits for the user to input a value using the "cin" statement. The value entered by the user is then stored in the "num1" variable. The program then prompts the user to enter the second number using another "cout" statement, and waits for the user to input a value using another "cin" statement. The value entered by the user is then stored in the "num2" variable.

After the two numbers are inputted, the program adds them together using the "+" operator and stores the result in the "sum" variable. Finally, the program outputs the sum of the two numbers using the "cout" statement, along with a message indicating that the output is the sum of the two numbers. The long long data type used in this program can handle larger numbers than the int data type, which is used in the previous example, making it suitable for adding very large numbers. The "return 0" statement at the end of the program indicates that the program has executed successfully.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA