Square number java: In the previous article, we have discussed Java Program to Generate Random Number
In this article we are going to understand what perfect square in java, Perfect Square number is and how we can check whether a number is Perfect Square or not in java or a perfect square program in Java and perfect square number in java with examples. Perfect square java program explained well in the given content.
Program to Check a Given Number is Perfect Square
Perfect Squares are numbers with whole roots.
Example :
- 16 = 42 Perfect Square number
- 9 = 32 Perfect Square number
- 6 = 3 *2 Not Perfect Square number
In the above examples the numbers 16 and 9 are only Perfect Squares. 6 is not a Perfect Square number.
Perfect Square Number Program In Java, Let’s see different ways to check if a given number is Perfect Square or not.
Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.
Approach :
- Enter a number or Declare a number.
- The number is passed into the square root function and the result is passed to the floor function to raise the double it to the next integer.
- Then it is compared with the number, if both are same then it is said to be a perfect square number.
Method-1: Java Program to Check a Given Number is Perfect Square By Using Static Value
import java.util.Scanner; public class PerfectSquareNumber { public static void main(String args[]) { //A number declared int num = 81; //checking the number is perfect square or not. double root = Math.sqrt(num); //if the condition satisfies then given number is perfect square if((Math.floor(root) - root)==0) System.out.println(num+" is a Perfect Square Number"); // else given number is not the perfect square else System.out.println(num+" is Not a Perfect Square Number"); } }
Output: 81 is a Perfect Square Number
Method-2: Java Program to Check a Given Number is Perfect Square By User Input Value
import java.util.Scanner; public class PerfectSquareNumber { public static void main(String args[]) { //Taking the number as input from the user using scanner class Scanner scan = new Scanner(System.in); System.out.print("Enter a number : "); int num = scan.nextInt(); //checking the number is perfect square or not. double root = Math.sqrt(num); //if the condition satisfies then given number is perfect square if((Math.floor(root) - root)==0) System.out.println(num+" is a Perfect Square Number"); // else given number is not the perfect square else System.out.println(num+" is Not a Perfect Square Number"); } }
Output: Enter a number : 64 64 is a Perfect Square Number
Method-3: Java Program to Check a Given Number is Perfect Square By User Defined Method
Program To Check Whether A Number Is Perfect Square Or Not In Java
import java.util.Scanner; public class PerfectSquareNumber { public static void main(String args[]) { //Taking the number as input from the user using scanner class Scanner scan = new Scanner(System.in); System.out.print("Enter a number : "); int num = scan.nextInt(); //isPerfectSquare() user defined method is called to check perfect square or not //and return value is true or false stored in a boolean variable 'flag' boolean flag = isPerfectSquare(num); if(flag) { System.out.println(num+" is a Perfect Square Number"); } else { System.out.println(num+" is Not a Perfect Square Number"); } } // Program to check for perfectsquare numbers static boolean isPerfectSquare(int num) { double root = Math.sqrt(num); //if the condition satisfies then given number is perfect square and returns true if((Math.floor(root) - root)==0) return true; // else given number is not the perfect square and returns false else return false; } }
Output: Case-1 Enter a number :16 16 is a Perfect Square Number Case-2 Enter a number :167 167 is Not a Perfect Square Number
Understand the Programming Language Java and learn the language fastly by using our wide range of Java Programming Examples with Output and try to write programs on your own.
Read Also: Java Program to Display Alternate Prime Numbers
Practice yourself:
- Write A Program To Check Whether A Number Is Perfect Square Or Not In Java?
- Java Program To Check If A Given Number Is Perfect Square?
- Java Program To Find Perfect Square?
- Check If Number Is Perfect Square Java?
- How To Find Perfect Square In Java?
- How To Find A Number Is Perfect Square Or Not In Java?
- How To Check If A Number Is Perfect Square In Java?
- How To Check Perfect Square In Java?
- Perfect Square Numbers Program In Java?
- Java Program To Check If Given Number Is Perfect Square?
Related Java Programs: