2. Design a Do-While loop that asks the user to enter two numbers. The numbers should be added and the sum displayed. The loop should ask the user whether he or she wishes to perform the operation again. If so, the loop should repeat; otherwise it should terminate.

Respuesta :

#include <iostream>

#include <string>

main ()

{

   using std::cout;

   using std::cin;

   std::string s;

   do

   {

      cout<<"Type 2 numbers: ";

      int a,b;

      cin>>a>>b;

      cout<<"Sum of your numbers equals to: "<<a+b;

      cout<<"\nType \"Yes\" if you want to continue.";

      cin>>s;

   }while(s=="Yes");

   return 0;

}

Answer:

Program :

#include <iostream> // Header file.

using namespace std;

int main () // Main function which starts the executions

{

   int First_Number,Second_Number; // variable declaration.

   char choice; // Variable declaration.

   do // do while loop

   {

        cout<<"Enter two number for addition"; // user instruction

        cin>>First_Number>>Second_Number; // take a input

        cout<<"The adition of "<<First_Number<<" and "<<Second_Number<<" is: "<<First_Number+Second_Number; //Addition will print

        cout<<"\nType \"Y\" for continue the operation again otherwise press any key"; // User message for choice.

        cin>>choice; // input for choice.

   }

   while(choice=='Y'||choice=='y'); // condition for Do-While loop

   return 0; // return statement.

}

Output:

  • If the user input are 4,5 and u then the output is 9
  • If the user input are 5,6 and y and 7,8 and u then the output are 11, 15.

Explanation:

  • The above code is in C++ language.
  • The above program firstly renders a message to enter the two numbers.
  • Then it performs addition operation and then the result will be printed.
  • Then it renders a message for the user choice.
  • If the user enters capital y or small y, then the above operation will continue again.
  • Otherwise, the program will be terminated.