-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCheckPerfectSquare.java
More file actions
32 lines (32 loc) · 916 Bytes
/
CheckPerfectSquare.java
File metadata and controls
32 lines (32 loc) · 916 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.Scanner;
public class CheckPerfectSquare
{
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;
}
}