Problem Statement
Write a Java program to check whether a given number is a perfect square or not
Condition
Input will only be a positive interger and will not have any decimal or special characters
Input
9
Output
TRUE
Explanation
A positive integer is a perfect square if it is the product of some integer with itself. As 9 is a square of 3 (ie., 3 * 3 = 9).
code:
import java.util.Scanner;
class Solution{
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int number = s.nextInt();
s.close();
double result = Math.sqrt(number);
if((int) result == result){
System.out.println("TRUE");
}
else System.out.println("FALSE");
}
}
Comments
Post a Comment