forked from client69/Open
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Automorphic Number
42 lines (34 loc) · 1 KB
/
Automorphic Number
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
36
37
38
39
40
41
42
Given a number N, the task is to check whether the number is Automorphic number or not.
A number is called Automorphic number if and only if its square ends in the same digits as the number itself.
Input : N = 25
Output : Automorphic
As 25*25 = 625
Input : N = 7
Output : Not Automorphic
As 7*7 = 49
SOLUTION:
class Test {
// Function to check Automorphic number
static boolean isAutomorphic(int N)
{
// Store the square
int sq = N * N;
// Start Comparing digits
while (N > 0) {
// Return false, if any digit of N doesn't
// match with its square's digits from last
if (N % 10 != sq % 10)
return false;
// Reduce N and square
N /= 10;
sq /= 10;
}
return true;
}
// Driver method
public static void main(String[] args)
{
int N = 5;
System.out.println(isAutomorphic(N) ? "Automorphic" : "Not Automorphic");
}
}