11
#include <iostream>
using namespace std;
int main() {
int num1, num2, sum;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
sum = num1 + num2;
cout << "Sum of " << num1 << " and " << num2 << " is: " << sum << endl;
return 0;
}
Explanation:
#include <iostream>
: Includes the Input/Output Stream Library to perform input and output operations in C++.using namespace std;
: Allows elements of the standard C++ library (e.g., cout, cin) to be used without explicitly qualifying them with 'std::'.int main() { ... }
: The main function where the program execution starts.int num1, num2, sum;
: Declares three integer variablesnum1
,num2
, andsum
to store user input and the sum of the two numbers.cout << "Enter first number: ";
: Displays a message asking the user to enter the first number.cin >> num1;
: Reads the user input for the first number and stores it in the variablenum1
.cout << "Enter second number: ";
: Displays a message asking the user to enter the second number.cin >> num2;
: Reads the user input for the second number and stores it in the variablenum2
.sum = num1 + num2;
: Calculates the sum ofnum1
andnum2
and stores the result in the variablesum
.cout << "Sum of " << num1 << " and " << num2 << " is: " << sum << endl;
: Displays the sum ofnum1
andnum2
usingcout
and concatenates the numbers and the result in the output message.return 0;
: Indicates successful program execution and terminates themain()
function.