Examine the program below that aims to allow a user to guess the square of a number that is entered. Part of the code has been replaced by a comment: import java.util.Scanner; public class IterationQ7...

Examine the program below that aims to allow a user to guess the square of a number that is entered. Part of the code has been replaced by a comment: import java.util.Scanner; public class IterationQ7 { public static void main(String[] args) { int num, square; Scanner keyboard = new Scanner(System.in); System.out.print("Enter a number "); num = keyboard.nextInt(); System.out.print("Enter the square of this number "); square = keyboard.nextInt(); // loop to check answer while (/* test to be completed */) { System.out.println("Wrong answer, try again"); square = keyboard.nextInt(); } System.out.println("Well done, right answer"); } } (a) Why is a while loop preferable to a for loop or a do…while loop here? (b) Replace the comment with an appropriate test for this loop. What would be the output of the following program? { public static void main(String[] args) { for(int i=1; i { if (i > 5) { break; } System.out.println(i); } } }

May 19, 2022
SOLUTION.PDF

Get Answer To This Question

Submit New Assignment

Copy and Paste Your Assignment Here