Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class PerfectSquare {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.println("Perfect square identifier (Enter -1 to quit program)");
- int userInput;
- do {
- System.out.print("Please enter a number: ");
- userInput = Integer.parseInt(sc.nextLine());
- if (!(perfectSquareIdentifier(userInput))) {
- System.out.printf("%,d is NOT a perfect square.\n", userInput);
- } else {
- System.out.printf("%,d is a perfect square.\n", userInput);
- }
- } while (userInput != -1);
- } // end main
- public static boolean perfectSquareIdentifier(int number) {
- if (number < 0) {
- return false;
- } else {
- double squareRootOfNumber = Math.sqrt(number);
- return ((squareRootOfNumber - Math.floor(squareRootOfNumber)) == 0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement