-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutomorphic_Number.java
38 lines (35 loc) · 1.61 KB
/
Automorphic_Number.java
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
// In this section, we will learn automorphic numbers with examples and also
// create Java programs that check whether the number is automorphic or not.
// What is an automorphic number?
// A number is called an automorphic number if and only if the square of the
// given number ends with the same number itself. For example, 25, 76 are
// automorphic numbers because their square is 625 and 5776, respectively and
// the last two digits of the square represent the number itself. Some other
// automorphic numbers are 5, 6, 36, 890625, etc.
class Automorphic_Number {
// user-defined static method that checks whether the number is automorphic or
// not
static boolean isAutomorphic(int num) {
// determines the square of the specified number
int square = num * num;
// comparing the digits until the number becomes 0
while (num > 0) {
// find the remainder (last digit) of the variable num and square and comparing
// them
if (num % 10 != square % 10)
// returns false if digits are not equal
return false;
// reduce num and square by dividing them by 10
num = num / 10;
square = square / 10;
}
return true;
}
// Driver code
public static void main(String args[]) {
// number to be check
// calling the method and prints the result accordingly
System.out.println(isAutomorphic(76) ? "Automorphic" : "Not Automorphic");
System.out.println(isAutomorphic(13) ? "Automorphic" : "Not Automorphic");
}
}