Ask 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 if he or she wishes to per - form the operation again. If so, the loop should repeat; otherwise it should terminate.

+4
Answers (1)
  1. 4 February, 02:18
    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?
Find 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 the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers