-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutomorphic.java
More file actions
31 lines (24 loc) · 825 Bytes
/
Copy pathAutomorphic.java
File metadata and controls
31 lines (24 loc) · 825 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
import java.util.Scanner;
public class Automorphic{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number to check if it's automorphic: ");
int number = sc.nextInt();
int square = number * number;
if (isAutomorphic(number, square)) {
System.out.println(number + " is an automorphic number.");
} else {
System.out.println(number + " is not an automorphic number.");
}
}
public static boolean isAutomorphic(int number, int square) {
int temp = number;
int cnt = 0;
while (temp > 0) {
cnt++;
temp = temp / 10;
}
int lastDigits = square % (int) Math.pow(10, cnt);
return lastDigits == number;
}
}