Advertisement
binibiningtinamoran

PerfectSquare

Oct 29th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class PerfectSquare {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner sc = new Scanner(System.in);
  8.  
  9.         System.out.println("Perfect square identifier (Enter -1 to quit program)");
  10.  
  11.         int userInput;
  12.  
  13.         do {
  14.             System.out.print("Please enter a number: ");
  15.             userInput = Integer.parseInt(sc.nextLine());
  16.             if (!(perfectSquareIdentifier(userInput))) {
  17.                 System.out.printf("%,d is NOT a perfect square.\n", userInput);
  18.             } else {
  19.                 System.out.printf("%,d is a perfect square.\n", userInput);
  20.             }
  21.         } while (userInput != -1);
  22.     } // end main
  23.  
  24.     public static boolean perfectSquareIdentifier(int number) {
  25.         if (number < 0) {
  26.             return false;
  27.         } else {
  28.             double squareRootOfNumber = Math.sqrt(number);
  29.             return ((squareRootOfNumber - Math.floor(squareRootOfNumber)) == 0);
  30.         }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement