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
Show 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