Write a C++ Program to Add Two Numbers

This program declares three variables: num1 and num2 to hold the two numbers entered by the user, and sum to hold their sum. It then uses the cout object to print prompts asking the user to enter the two numbers, and the cin object to read the numbers from the standard input stream. Finally, it calculates the sum of the two numbers using the + operator and prints the result using cout.

C++ example with Solutions:

I hope this helps! Let me know if you have any questions.

#include <iostream>

int main()
{
    int num1, num2, sum; // Declare variables to hold the two numbers and their sum

    std::cout << "Enter the first number: "; // Prompt the user to enter the first number
    std::cin >> num1; // Read the first number from the standard input stream

    std::cout << "Enter the second number: "; // Prompt the user to enter the second number
    std::cin >> num2; // Read the second number from the standard input stream

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

    std::cout << "The sum of the two numbers is: " << sum << std::endl; // Print the sum

    return 0;
}