softwareegg.courses4u
#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 for basic input and output operations.
- using namespace std;
: Declares that the program will use the standard namespace for shorthand access to standard library elements.
- int main() {...}
: The main function where the execution of the program begins.
- int num1, num2, sum;
: Declares three integer variables num1
, num2
, and sum
.
- cout << "Enter first number: ";
: Outputs a message prompting the user to enter the first number.
- cin >> num1;
: Takes user input from the keyboard and assigns it to the variable num1
.
- cout << "Enter second number: ";
: Outputs a message prompting the user to enter the second number.
- cin >> num2;
: Takes user input from the keyboard and assigns it to the variable num2
.
- sum = num1 + num2;
: Calculates the sum of num1
and num2
and stores it in the variable sum
.
- cout << "Sum of " << num1 << " and " << num2 << " is: " << sum << endl;
: Outputs the sum of num1
and num2
along with the numbers themselves.
- return 0;
: Indicates that the program executed successfully and returns 0 to the operating system.