Java Program to Check if a Given Number is Perfect Square or Not

In this section, we have developed several Java programs to determine whether a given number is a perfect square or not.

A perfect square, also known as a square number, is a positive integer that is obtained by multiplying two identical integers together. To put it simply, a perfect square is the result of multiplying a positive integer by itself.

Perfect square numbers have a distinct property in that their last digits can only be 0, 1, 4, 6, 9, or 5. Here are some examples of perfect square numbers:

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

In Java, we can employ the following method to verify whether a number is a perfect square or not.

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

Using sqrt() Method

The approach we have utilized is as follows:

  1. Determine the square root of the given number.
  2. Calculate the floor value of the obtained square root.
  3. Compute the difference between the floor value and the square root obtained in step 1.
  4. Finally, compare the value obtained in step 3 with 0. If the value is equal to 0, then the given number is a perfect square; otherwise, it is not.

Let’s illustrate the above steps with an example.

Example: Determine if the number 324 is a perfect square or not.

  1. The square root of 324 is 18.
  2. The floor value of the square root is 18.
  3. The difference between the square root and floor value is 0.
  4. Since the difference is equal to 0, we can conclude that the given number is a perfect square.

We can implement the above steps in a Java program using the following predefined methods from the Math class:

sqrt(): This is a static method of the Math class that can be called directly using the class name. It takes a double parameter and returns the positive square root of the given value. If the parameter is negative or NaN, it returns NaN.

syntax:

public static double sqrt(double a)

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

syntax:

public static double floor(double a)

In the following example, we have implemented a user-defined method called checkPerfectSquare(). This method takes a double argument and checks whether the number is a perfect square.

Inside the method, we calculate the square root of the number using the Math.sqrt() method and assign it to a variable called sqrt. Then, we calculate the floor value of the calculated square root using the Math.floor() method. Additionally, we calculate the difference between the square root and floor values. Finally, we compare the resulting difference with 0 and return a boolean value.

The value returned by the checkPerfectSquare() method is utilized within the calling method, specifically within an if statement. If the difference calculated is equal to 0, it indicates that the given number is a perfect square. else, it signifies that the number is not a perfect square.

CheckPerfectSquareExample.java

import java.util.Scanner;  
public class CheckPerfectSquareExample  
{   
//user-defined method that checks the number is perfect square or not  
static boolean checkPerfectSquare(double number)    
{   
//calculating the square root of the given number  
double sqrt=Math.sqrt(number);   
//finds the floor value of the square root and comparing it with zero  
return ((sqrt - Math.floor(sqrt)) == 0);   
}   
//main method  
public static void main(String[] args)    
{   
System.out.print("Enter any number: ");  
//object of the Scanner class  
Scanner sc=new Scanner(System.in);  
//reading a number of type double from the user  
double number=sc.nextDouble();   
//calling the user defined method  
if (checkPerfectSquare(number))   
System.out.print("Yes, the given number is perfect square.");   
else  
System.out.print("No, the given number is not perfect square.");   
}   
}
Enter any number: 1600
Yes, the given number is perfect square.

 Output 2:

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

Using User-Defined Logic

In the given example, we have implemented our logic to determine whether a number is a perfect square.

CheckPerfectSquareExample1.java

import java.util.Scanner;  
public class CheckPerfectSquareExample1  
{  
public static void main(String[] args)   
{   
//object of the Scanner class  
Scanner sc=new Scanner(System.in);  
System.out.print("Enter a number: ");   
//reading a number of type double from the user  
double number=sc.nextDouble();   
if (checkPerfectSquare(number))   
System.out.println("Yes, the given number is perfect square.");  
else  
System.out.print("No, the given number is not perfect square.");   
}  
//user-defined method that checks the number is perfect square or not   
static boolean checkPerfectSquare(double number)   
{   
for (int i=1; i*i<=number; i++)   
{   
//if (i * i = number)   
//comparing conditions using logical AND  
if((number%i==0) && (number/i==i))   
{   
//returns true if both conditions are true      
return true;   
}   
}   
//returns false if any one condition is false  
return false;   
}   
}

Output 1:

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

Output 2:

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

In the given example, the remainder of a number is first calculated using the modulo operator. Subsequently, the remainder is compared with the numbers 2, 3, 7, and 8 using the Logical OR operator. This comparison is done because perfect squares never end with these specific numbers. If the remainder of the number is 2, 3, 7, or 8, it is concluded that the number cannot be a perfect square.

On the other hand, if the remainder of the number is 0, 1, 4, 6, or 9, it proceeds to the for loop. Within each iteration of the loop, the square of the current value of ‘i’ is calculated by multiplying it by itself (i*i). If the square of ‘i’ is equal to the entered number ‘n’, it is determined that the number is a perfect square, and the function returns true. Otherwise, if no perfect square is found after all iterations, the function returns false.

CheckPerfectSquareExample2.java

import java.util.*;  
public class CheckPerfectSquareExample2   
{  
public static void main(String[] args)   
{  
//object of the Scanner class      
Scanner sc = new Scanner(System.in);  
System.out.print("Enter a number: ");  
//reading an integer from the user  
int number = sc.nextInt();  
//method calling inside the print statement  
System.out.print("Is the number perfect square? " +checkPerfectSquare(number));  
}  
public static boolean checkPerfectSquare(int number)   
{  
//calculating the remainder of the given number using the modulo operator  
int x=number % 10;  
//comparing the value of x with 2, 3, 7, and 8 using the Logical OR operator  
//perfect square never end with 2, 3, 7, and 8  
if (x==2 || x==3 || x==7 || x==8)   
{  
return false;  
}  
for (int i=0; i<=number/2 + 1; i++)   
{  
//type-casting the variable i and checking its equality with n  
if (i*i==number)   
{  
return true;  
}  
}  
return false;  
}  
}

Output 1:

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

Output 2:

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

CheckPerfectSquareExample3.java

import java.util.Scanner;  
public class CheckPerfectSquareExample3  
{  
public static void main(String[] args)  
{  
//creating Scanner class object  
Scanner sc = new Scanner(System.in);  
System.out.print("Enter a number: ");  
//taking an integer as input  
int number = sc.nextInt();   
//calculates the square root of the number  
double x = Math.sqrt(number);   
//typecast the root from double to int  
if(x == (int)x)  
{  
System.out.println(number+" is a perfect square.");  
}  
else  
{  
System.out.println(number+" is not a perfect square.");  
}  
}  
}

Output 1:

Enter a number: 121
121 is a perfect square.

Output 2:

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

In the provided program, we begin by taking the square root of the input number and converting it to an integer using type-casting. Subsequently, we calculate the square of this resulting integer and compare it with the original number. If the squared value is equal to the input number, it indicates that we have a perfect square.

CheckPerfectSquareExample4.java

import java.io.BufferedReader;  
import java.io.IOException;  
import java.io.InputStreamReader;  
public class CheckPerfectSquareExample4  
{  
public static void main(String[] args) throws IOException  
{  
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));  
System.out.print("Please enter an integer: ");  
//reading a number form the user   
int n=Integer.parseInt(reader.readLine());  
//finds the square root of the number and type-cast the square root in to int  
int sqrt = (int) Math.sqrt(n);  
//comparing the square of the sqrt with the number  
if(sqrt*sqrt == n)   
{  
System.out.println(n+" is a perfect square number.");  
}  
else   
{  
System.out.println(n+" is not a perfect square number.");  
}  
}  
}

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.

In the given example, we employ the following approach to determine if a number is a perfect square:

Let’s assume ‘n’ is the number we want to check. We set up a for loop that iterates from 1 to n/2. During each iteration, we calculate the value of x by squaring the current value of ‘i’ (x = i*i). We consider three possible conditions for the variable x:

  1. If x is equal to n, it indicates that the given number is a perfect square.
  2. If x is greater than n, we conclude that n is not a perfect square.
  3. If neither of the above two conditions is true, we continue the loop.

CheckPerfectSquareExample5.java

public class CheckPerfectSquareExample5  
{  
//user defined mehod  
public static boolean checkPerfectSquare(int number)  
{  
//comparing the number with 0 and 1  
//returns true if any one condign is true because 0 and 1 are perfect square   
if(number==0||number==1)  
return true;  
for (int i=0; i<=number/2; i++)   
{  
//for each iteration of i calculating the square      
int x=i*i;  
//the number is perfect square if x is equal to number  
if(x==number)  
return true;  
//the number cannot be perfect square if number<x  
else if (number<x)  
return false;  
else  
continue;  
}  
return false;  
}  
public static void main(String[] args)   
{  
int number=50;  
//calling the method and prints the result accordingly  
System.out.println(number + " is a perfect square number? " + checkPerfectSquare(number));  
number=361;  
System.out.println(number + " is a perfect square number? " + checkPerfectSquare(number));  
}  
}

Output:

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

Overall, this program provides a simple and effective method to determine if a given number is a perfect square in the Java programming language. Follow tutorials.freshersnow.com to learn more.