Write a complete \(\mathrm{C}++\) program that reads in two whole numbers and outputs their sum. Be sure to prompt for input, echo input, and label all output.

Short Answer

Expert verified
Question: Write a C++ program that reads in two whole numbers and outputs their sum. Answer: The C++ program below reads two whole numbers and outputs their sum: ```cpp #include using namespace std; int main() { cout << "Please enter the first whole number: "; int num1; cin >> num1; cout << "Please enter the second whole number: "; int num2; cin >> num2; int sum = num1 + num2; cout << "You entered the numbers " << num1 << " and " << num2 << "." << endl; cout << "The sum of these numbers is: " << sum << endl; return 0; } ```

Step by step solution

01

Including necessary libraries

We need to include the iostream library in the program for input and output operation. ```cpp #include ```
02

Using the standard namespace

To avoid writing 'std::' before every cin and cout statement, we'll use the 'std' namespace. ```cpp using namespace std; ```
03

Writing the main function

The main function is the starting point of our program. ```cpp int main() { // Code will be placed here return 0; } ```
04

Prompting for input

Prompt the user to enter the first and second whole numbers. ```cpp cout << "Please enter the first whole number: "; cout << "Please enter the second whole number: "; ```
05

Reading the input values

Declare two integer variables 'num1' and 'num2' and use 'cin' to read the input. ```cpp int num1, num2; cin >> num1; cin >> num2; ```
06

Calculating the sum

Declare another integer variable 'sum' and store the result of addition for 'num1' and 'num2' into 'sum'. ```cpp int sum = num1 + num2; ```
07

Echoing the input values

Output the entered values before displaying the result. ```cpp cout << "You entered the numbers " << num1 << " and " << num2 << "." << endl; ```
08

Outputting the final result

Output the sum of the two input numbers. ```cpp cout << "The sum of these numbers is: " << sum << endl; ``` Combining all the above steps, the final C++ program would be: ```cpp #include using namespace std; int main() { cout << "Please enter the first whole number: "; int num1; cin >> num1; cout << "Please enter the second whole number: "; int num2; cin >> num2; int sum = num1 + num2; cout << "You entered the numbers " << num1 << " and " << num2 << "." << endl; cout << "The sum of these numbers is: " << sum << endl; return 0; } ```

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Study anywhere. Anytime. Across all devices.

Sign-up for free