Write C++ program to add two numbers

Here is an example of a C++ program that prompts the user to enter two numbers using the “cout” function, which is followed by “cin” function to read the input from the user.

The program then adds the two numbers together using the “+” operator and stores the result in the variable result. The program then uses the “cout” function to print the sum to the console.

#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 " << num1 << " and " << num2 << " is " << sum << endl;

    return 0;
}

Output:

Enter the first number:4
Enter the second number:5
The sum of 4 and 5 is 9

This program uses the “cin” and “cout” functions from the “iostream” library to handle input and output. The “cin” function is used to read input from the user, and the “cout” function is used to print output to the console.

The “+” operator is used to add the two numbers together, and the result is stored in the variable “sum”. Finally, the program uses the “cout” function to print the sum to the console.

Leave a Comment