# Java Program to Check if a Given Number is Perfect Square

In this section, we have created a number of Java program to check if a given number is perfect square or not.

The perfect square or square number is a positive integer that is square of an integer. In other words, when we multiply two same numbers together, then the product that we get is called the perfect square. In short, it is the product of two positive equal integers or product of an integer with itself.

The property of the perfect square number is that it ends only with 0, 1, 4, 6, 9, and 25. The examples of perfect square are:

```49=7*7
100=10*10
625=25*25
```

In Java, we can use the following way to check if a number is perfect square or not.

• Using sqrt() method
• Using User-Defined Logic

## Using sqrt() Method

The approach, we have followed is:

1. First, find out the square root of the given number.
2. Calculate the floor value of the calculated square root.
3. Find the difference of the floor value with the square root that we have find in step 1.
4. At last, compare the value (that we get in step 3) with 0. If the value is equal to 0 the given number is perfect square, else not.

Let’s understand the above stapes through an example.

Example: Check the number 324 is perfect square or not.

• The square root of 324 is 18.
• The floor value of the square root is 18.
• The difference of square root and floor value is 0.
• The difference is equal to 0. Hence, the given number is perfect square.

Let’s implement the above steps in a Java program.

To find the square root and floor value of the number, we have used the following predefined methods of the Java Math class.

sqrt(): It is a static method of the Math class. We can call it directly by using the class name. It parses a parameter of type double. If we pass a number less than zero or NaN, it returns NaN. It returns a positive square root of a double value.

syntax:

1. public static double sqrt(double a)

floor(): It is also a static method of the Math class. It calculates the floor value of the number. It parses a parameter of type double. It returns the largest (closest positive integer) floating-point value that is less than or equal to the argument and is equal to a mathematical integer.

syntax:

1. public static double floor(double a)

In the following example, we have created a user-defined method named checkPerfectSquare(). It parses an argument of type double. The method contains the logic to check if a number is perfect square.

In the method, we have calculated the square root of the number by the Math.sqrt() method and store it in a variable named sqrt. After that, we have found the floor value of the calculated square root by the Math.floor() method. Along with this, we have found the difference between the two sqrt and floor value. The resultant of the difference compared with 0 and returns a Boolean value.

The returned value passed to the calling method (that is inside if statement). If the difference is equal to 0, the given number is perfect square, else not a perfect square.

CheckPerfectSquareExample1.java

1. import java.util.Scanner;
2. public class CheckPerfectSquareExample1
3. {
4. //user-defined method that checks the number is perfect square or not
5. static boolean checkPerfectSquare(double number)
6. {
7. //calculating the square root of the given number
8. double sqrt=Math.sqrt(number);
9. //finds the floor value of the square root and comparing it with zero
10. return ((sqrt – Math.floor(sqrt)) == 0);
11. }
12. //main method
13. public static void main(String[] args)
14. {
15. System.out.print(“Enter any number: “);
16. //object of the Scanner class
17. Scanner sc=new Scanner(System.in);
18. //reading a number of type double from the user
19. double number=sc.nextDouble();
20. //calling the user defined method
21. if (checkPerfectSquare(number))
22. System.out.print(“Yes, the given number is perfect square.”);
23. else
24. System.out.print(“No, the given number is not perfect square.”);
25. }
26. }

Output 1:

```Enter any number: 8100
Yes, the given number is perfect square.
```

Output 2:

```Enter any number: 24
No, the given number is not perfect square.
```

## Using User-Defined Logic

In the following example, we have not used any predefined method of the Java Math class. We have implemented own logic to check the perfect square.

CheckPerfectSquareExample2.java

1. import java.util.Scanner;
2. public class CheckPerfectSquareExample2
3. {
4. public static void main(String[] args)
5. {
6. //object of the Scanner class
7. Scanner sc=new Scanner(System.in);
8. System.out.print(“Enter a number: “);
9. //reading a number of type double from the user
10. double number=sc.nextDouble();
11. if (checkPerfectSquare(number))
12. System.out.println(“Yes, the given number is perfect square.”);
13. else
14. System.out.print(“No, the given number is not perfect square.”);
15. }
16. //user-defined method that checks the number is perfect square or not
17. static boolean checkPerfectSquare(double number)
18. {
19. for (int i=1; i*i<=number; i++)
20. {
21. //if (i * i = number)
22. //comparing conditions using logical AND
23. if((number%i==0) && (number/i==i))
24. {
25. //returns true if both conditions are true
26. return true;
27. }
28. }
29. //returns false if any one condition is false
30. return false;
31. }
32. }

Output 1:

```Enter a number: 121
Yes, the given number is perfect square.
```

Output 2:

```Enter a number: 121
No, the given number is not perfect square.
```

In the following example, first we have calculated the remainder of the given number using the modulo operator. After that, we have compared the remainder with 2, 3, 7, and 8 using the Logical OR operator because the perfect square never ends with these numbers. If the number has the remainder 2, 3, 7, or 8, it cannot be the perfect square.

If the remainder of a number is 0, 1, 4, 6, and 9 it jumps to the for loop. For each iteration of i, we have calculated the i*i. If the square of i is equal to the entered number (n), the number is perfect square and it returns true, else returns false.

CheckPerfectSquareExample3.java

1. import java.util.*;
2. public class CheckPerfectSquareExample3
3. {
4. public static void main(String[] args)
5. {
6. //object of the Scanner class
7. Scanner sc = new Scanner(System.in);
8. System.out.print(“Enter a number: “);
9. //reading an integer from the user
10. int number = sc.nextInt();
11. //method calling inside the print statement
12. System.out.print(“Is the number perfect square? ” +checkPerfectSquare(number));
13. }
14. public static boolean checkPerfectSquare(int number)
15. {
16. //calculating the remainder of the given number using the modulo operator
17. int x=number % 10;
18. //comparing the value of x with 2, 3, 7, and 8 using the Logical OR operator
19. //perfect square never end with 2, 3, 7, and 8
20. if (x==2 || x==3 || x==7 || x==8)
21. {
22. return false;
23. }
24. for (int i=0; i<=number/2 + 1; i++)
25. {
26. //type-casting the variable i and checking its equality with n
27. if (i*i==number)
28. {
29. return true;
30. }
31. }
32. return false;
33. }
34. }

Output 1:

```Enter a number: 121
Is the number perfect square? true
```

Output 2:

```Enter a number: 167
Is the number perfect square? false
```

Let’s see another logic.

CheckPerfectSquareExample4.java

1. import java.util.Scanner;
2. public class CheckPerfectSquareExample4
3. {
4. public static void main(String[] args)
5. {
6. //creating Scanner class object
7. Scanner sc = new Scanner(System.in);
8. System.out.print(“Enter a number: “);
9. //taking an integer as input
10. int number = sc.nextInt();
11. //calculates the square root of the number
12. double x = Math.sqrt(number);
13. //typecast the root from double to int
14. if(x == (int)x)
15. {
16. System.out.println(number+” is a perfect square.”);
17. }
18. else
19. {
20. System.out.println(number+” is not a perfect square.”);
21. }
22. }
23. }

Output 1:

```Enter a number: 121
121 is a perfect square.
```

Output 2:

```Enter a number: 143
143 is not a perfect square.
```

Let’s see another logic to check if a given number is perfect square or not.

In the following program, we have taken the square root of the number that we have entered and type-cast it into int. After that, we take the square of the value to see whether it is same as the number given. If they are equal to the number, we got a perfect square number.

CheckPerfectSquareExample5.java

2. import java.io.IOException;
4. public class CheckPerfectSquareExample5
5. {
6. public static void main(String[] args) throws IOException
7. {
9. System.out.print(“Please enter an integer: “);
10. //reading a number form the user
12. //finds the square root of the number and type-cast the square root in to int
13. int sqrt = (int) Math.sqrt(n);
14. //comparing the square of the sqrt with the number
15. if(sqrt*sqrt == n)
16. {
17. System.out.println(n+” is a perfect square number.”);
18. }
19. else
20. {
21. System.out.println(n+” is not a perfect square number.”);
22. }
23. }
24. }

Output 1:

```Please enter an integer: 98
98 is not a perfect square number.
```

Output 2:

```Please enter an integer: 529
529 is a perfect square number.
```

Let’s see another example with another logic.

In the following example, we have used the approach, given below:

1. Suppose, n is a number that we want to check.
2. Create a for loop that executes from 1 to n/2.
3. Calculate x=i*i, for each iteration.
4. The variable x has the following three conditions:
• If x=n, the given number is perfect square.
• If x>n, n is not a perfect square.
• Continue if the above two conditions are not true.

CheckPerfectSquareExample6.java

1. public class CheckPerfectSquareExample6
2. {
3. //user defined mehod
4. public static boolean checkPerfectSquare(int number)
5. {
6. //comparing the number with 0 and 1
7. //returns true if any one condign is true because 0 and 1 are perfect square
8. if(number==0||number==1)
9. return true;
10. for (int i=0; i<=number/2; i++)
11. {
12. //for each iteration of i calculating the square
13. int x=i*i;
14. //the number is perfect square if x is equal to number
15. if(x==number)
16. return true;
17. //the number cannot be perfect square if number<x
18. else if (number<x)
19. return false;
20. else
21. continue;
22. }
23. return false;
24. }
25. public static void main(String[] args)
26. {
27. int number=50;
28. //calling the method and prints the result accordingly
29. System.out.println(number + ” is a perfect square number? ” + checkPerfectSquare(number));
30. number=361;
31. System.out.println(number + ” is a perfect square number? ” + checkPerfectSquare(number));
32. }
33. }

Output:

```50 is a perfect square number? false
361 is a perfect square number? true```