161k views
3 votes
Write a do while loop that asks the user to enter two numbers. the numbers should be added and the sum displayed. Then the user should be asked if he or she wishes to perform the operations again. If so, the loop should repeat, otherwise it should terminate. can someone put this in a flowchart using visio?

1 Answer

1 vote

Answer:

The program to this question can be given as:

Program:

import java.util.*; //import package

public class Main //define main class

{

public static void main(String[] ar) //define main method

{

int a,b,sum=0; //define variables.

char choice;

Scanner ob = new Scanner(System.in); //creating Scanner class object.

do //loop

{

System.out.print("Enter the first number: "); //message

a=ob.nextInt(); //taking number by user input.

System.out.print("Enter the second number: "); //message

b=ob.nextInt(); //taking number by user input.

sum =a+b; //add numbers

System.out.println("Sum of numbers: " + sum); //print sum.

System.out.print("Do you want to continue y/n? "); //message

choice =ob.next().charAt(0); //tacking Character as input.

}

while(choice=='y' || choice == 'Y'); //check condition in loop.

}

}

Output:

Enter the first number: 33

Enter the second number: 36

Sum of numbers: 69

Do you want to continue y/n? y

Enter the first number: 23

Enter the second number: 76

Sum of numbers: 99

Do you want to continue y/n? n

Explanation:

In the above program firstly we import the java util package for user input then we define class in this class we define main method in the main method we define variable and create the scanner class object and then we define the loop that is do-while loop it is an exit control loop in this loop we take user input in the variable a and b. Then add number and put the value in the sum variable and print the value. In this loop at the last, we take char input by the user and check in the while section for another input if user input y or Y the program will restart otherwise it will terminate.

Write a do while loop that asks the user to enter two numbers. the numbers should-example-1
User Dbaer
by
5.3k points