Java Program to Check if a Given Number is Perfect SquareIn 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() MethodThe approach, we have followed is:
Let's understand the above stapes through an example. Example: Check the number 324 is perfect square or not.
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: 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: 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 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 LogicIn 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 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 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 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 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:
CheckPerfectSquareExample6.java Output: 50 is a perfect square number? false 361 is a perfect square number? true
Next TopicJava Programs
|