Ask Question
18 October, 06:32

Create 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

Sample Run1

Enter two numbers: 3 15

Do you want another operation: Yes

Enter two numbers: 45 56

Do you want another operation: No

Output1: Sum = 119

Sample Run2

Enter two numbers: 33 150

Do you want another operation: Yes

Enter two numbers: - 56 56

Do you want another operation: Yes

Enter two numbers: 58 15

Do you want another operation: Yes

Enter two numbers: 123 87

Do you want another operation: No

Output2: Sum = 466

+5
Answers (1)
  1. 18 October, 06:52
    0
    import java. util. Scanner;

    public class TestClock {

    public static void main (String[] args) {

    Scanner in = new Scanner (System. in);

    int sum = 0;

    char op;

    do{

    System. out. println ("Enter two numbers");

    int num1 = in. nextInt ();

    int num2 = in. nextInt ();

    sum = sum+num1+num2;

    System. out. println ("Do you wish to perform another operation, Y/N");

    op = in. next (). charAt (0);

    }while (op = ='Y'||op=='y');

    System. out. println ("sum "+sum);

    }

    }

    Explanation:

    The code is implemented in Java progrmming language

    create an integer variable sum create a character variable op create a do while loop to request user to enter num1 and num2 Request the user to also enter a char Y/N (yes or no repectively) While the char is Y keep adding up the sum and prompt the user to enter two new numbers Else break out of the loop and print the sum
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Create 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 ...” 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