Question #267197

  1. Using a do...while() loop, continuously scan for random integers that will be inputted by the user and print out its square, separated in each line.
  2. Once the inputted value is 0, it will still print out its square value but should then terminate the loop afterwards. Use this concept in making your loop condition.

Expert's answer

import java.util.Scanner;
public class Sol {
  public static void main(String[] arg) {
    Scanner s = new Scanner(System.in);
    int i = 0; 
    while ( true ) {
      int j = s.nextInt();
      if (i != j)
        System.in.println(i*i);
      else {
        System.in.println(i*i);
        break;
      }
    }
  }
}
LATEST TUTORIALS
APPROVED BY CLIENTS