-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCheckPerfectSquareExample3.java
More file actions
35 lines (35 loc) · 1005 Bytes
/
CheckPerfectSquareExample3.java
File metadata and controls
35 lines (35 loc) · 1005 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
33
34
35
// Checking if the number is perfect square or not.
import java.util.*;
public class CheckPerfectSquareExample3
{
public static void main(String[] args)
{
//object of the Scanner class
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
//reading an integer from the user
int number = sc.nextInt();
//method calling inside the print statement
System.out.print("Is the number perfect square? " +checkPerfectSquare(number));
}
public static boolean checkPerfectSquare(int number)
{
//calculating the remainder of the given number using the modulo operator
int x=number % 10;
//comparing the value of x with 2, 3, 7, and 8 using the Logical OR operator
//perfect square never end with 2, 3, 7, and 8
if (x==2 || x==3 || x==7 || x==8)
{
return false;
}
for (int i=0; i<=number/2 + 1; i++)
{
//type-casting the variable i and checking its equality with n
if (i*i==number)
{
return true;
}
}
return false;
}
}