Ask Question
24 August, 17:02

Write a do-while loop that asks the user to enter two numbers. The numbers should be added and the sum displayed. The user should be asked if he or she wishes to per - form the operation again. If so, the loop should repeat; otherwise it should terminate.

+5
Answers (1)
  1. 24 August, 18:28
    0
    The do-while loop for the given problem is shown below.

    do

    {

    / / user asked to enter two numbers

    cout<<"Enter two numbers to be added."<
    cin>>num1;

    cin>>num2;

    sum = num1 + num2;

    / / sum of the numbers is displayed

    cout<<"Sum of the two numbers is "<
    / / user asked whether to perform the operation again

    cout<<"Do you wish to continue (y/n) ?"<
    cin>>choice;

    }while (choice! = 'n');

    The variables to hold the two numbers and their sum are declared as float so that the program should work well for both integers and floating numbers.

    float num1, num2, sum;

    The char variable is taken since it holds only a single-character input from the user, 'y' or 'n'.

    char choice;

    The whole program is given below.

    #include

    using namespace std;

    int main () {

    float num1, num2, sum;

    char choice;

    do

    {

    / / user asked to enter two numbers

    cout<<"Enter two numbers to be added."<
    cin>>num1;

    cin>>num2;

    sum = num1 + num2;

    / / sum of the numbers is displayed

    cout<<"Sum of the two numbers is "<
    / / user asked whether to perform the operation again

    cout<<"Do you wish to continue (y/n) ?"<
    cin>>choice;

    }while (choice! = 'n');

    cout<<"Quitting ... "<
    }
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Write a do-while loop that asks the user to enter two numbers. The numbers should be added and the sum displayed. The user should be asked ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers