Perfect Square Program in Java

In this post, we will develop a Java program to check whether the given number is a perfect square or not.

There are different ways to check the given number is a perfect square or not. You may say that the easiest to use sqrt() method. But it is not the only way to check the given number is a perfect number or not.

We can also do this using loop, and also by using addition. First, we will do develop the Java method using the sqrt() method.

Also see:- Java program to Find Square Root of a Number, Java program to check Perfect number

Perfect square program using sqrt() method

The Math.sqrt() method returns the square root of the number. If the result is an integer number then the given number is a perfect square else it is not.

import java.util.Scanner;

public class PerfectSquare {

   // method to check the given number is 
   // perfect number or not
   public static boolean isPerfectSquare(double number) {

      // calculate square root of the number
      double root = Math.sqrt(number);

      // check square root is an integer or not
      return (root - Math.floor(root) == 0);
   }

   // main method
   public static void main(String[] args) {

      // declare variables
      double number = 0.0;
      boolean result = false;

      // create Scanner class object to 
      // read input
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter a number::");
      number = scan.nextDouble();

      // check number
      result = isPerfectSquare(number);

      // display result
      if(result)
         System.out.println("Perfect Square");
      else
         System.out.println("Not a Perfect Square");

      // close Scanner class object
      scan.close();
   }
}

Output for the different test-cases:-

Enter a number::25
Perfect Square

Enter a number::90
Not a Perfect Square

Without using Math.sqrt() method

The idea is to iterate from 1 to N and check N*N is equal to the given number or not, if yes then it is a perfect square.

import java.util.Scanner;

public class PerfectSquare {

   // method to check the given number is 
   // perfect number or not
   public static boolean isPerfectSquare(int num){

      // loop to iterate
      for(int i=1; i*i<=num; i++) {

         // check for i
         if((num%i == 0) && (num/i==i))
         return true;
      }

      return false;
   }

   // main method
   public static void main(String[] args) {

      // declare variables
      int number = 0;
      boolean result = false;

      // create Scanner class object to 
      // read input
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter a number::");
      number = scan.nextInt();

      // check number
      result = isPerfectSquare(number);

      // display result
      if(result)
         System.out.println("Perfect Square");
      else
         System.out.println("Not a Perfect Square");

      // close Scanner class object
      scan.close();
   }
}

Perfect Square Program in Java using addition

We can also solve this problem using addition. The sum of first N odd numbers is a perfect number. For example:-

1 + 3 = 4 => 2 * 2
1 + 3 + 5 = 9 => 3*3
1 + 3 + 5 + 7 = 16 => 4*4
1 + 3 + 5 + 7 + 9 = 25 => 5*5
and so on…

If the number belongs to these values then it is a perfect square. For this take a loop, find the sum of odd numbers and then check for a perfect square.

import java.util.Scanner;

public class PerfectSquare {

   // method to check the given number is 
   // perfect number or not
   public static boolean isPerfectSquare(int num){

      // loop to iterate
      for(int sum=0, i=1; i <= num; i+=2) {

         // calculate sum
         sum += i;

         // compare sum and number
         if(sum == num)
         return true;
      }

      return false;
   }

   // main method
   public static void main(String[] args) {

      // declare variables
      int number = 0;
      boolean result = false;

      // create Scanner class object to 
      // read input
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter a number::");
      number = scan.nextInt();

      // check number
      result = isPerfectSquare(number);

      // display result
      if(result)
         System.out.println("Perfect Square");
      else
         System.out.println("Not a Perfect Square");

      // close Scanner class object
      scan.close();
   }
}

Java program to find Perfect Square between two numbers

import java.util.Scanner;

public class PerfectSquareInRange {

   // method to check the given number is 
   // perfect number or not
   public static boolean isPerfectSquare(double number) {

      // calculate square root of the number
      double root = Math.sqrt(number);

      // check square root is an integer or not
      return (root - Math.floor(root) == 0);
   }

   // main method
   public static void main(String[] args) {

      // declare variables
      int minRange = 0;
      int maxRange = 0;
      boolean count = false;

      // create Scanner class object to 
      // read input
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter min value of range::");
      minRange = scan.nextInt();
      System.out.print("Enter max value of range::");
      maxRange = scan.nextInt();

      System.out.println("The perfect square"
             + " between "+ minRange +" to "
            + maxRange + " are: ");

      // loop
      for(int i=minRange; i<=maxRange; i++) {
    	  if(isPerfectSquare(i)) {
    	      System.out.print(i+" ");
                  count = true;
    	  }
      }

      // if there is no any perfect square
      // in the given range
      if(!count)
         System.out.println("No perfect "
            + "square number found in this range");

      // close Scanner class object
      scan.close();
   }
}

The output for the different test-cases:-

Enter min value of range:: 1
Enter max value of range:: 100
The perfect square between 1 to 100 are:
1 4 9 16 25 36 49 64 81 100

Enter min value of range:: 65
Enter max value of range:: 80
The perfect square between 65 to 80 are:
No perfect square number found in this range

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply