Answer to Question #263540 in HTML/JavaScript Web Application for Hermen

Question #263540

Write a javascript program 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

1
Expert's answer
2021-11-09T17:46:56-0500
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 the Java programming language

  1. create an integer variable sum
  2. create a character variable op
  3. create a do-while loop to request the user to enter num1 and num2
  4. Request the user to also enter a char Y/N (yes or no respectively)
  5. While the char is Y keep adding up the sum and prompt the user to enter two new numbers
  6. Else break out of the loop and print the sum

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS