-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathAutomorphicNumber.java
More file actions
49 lines (42 loc) · 1.59 KB
/
AutomorphicNumber.java
File metadata and controls
49 lines (42 loc) · 1.59 KB
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
43
44
45
46
47
48
49
import java.util.Scanner;
/*
* An automorphic number is a number whose square ends with the number itself.
* For example, 5 is an automorphic number, 5 * 5 = 25, 6 * 6 = 36, 25 * 25 = 625 and so on.
*/
public class AutomorphicNumber {
public static void main(String[] args) {
// Initialize the scanner class to take input
Scanner sc = new Scanner(System.in);
// Prompt the user for input
System.out.print("Enter number to check if it is Automorphic or not : ");
// Take input
int num = sc.nextInt();
// Check if number is automorphic and print the desired message
if (checkAutomorphic(num))
System.out.println("This number is an Automorphic Number");
else
System.out.println("This number is not an Automorphic Number");
}
/**
* Check if the given number is automorphic or not
*
* @param number The number to check
* @return true if the number is automorphic, false otherwise
*/
public static boolean checkAutomorphic(int number) {
// Store the square of the number
int square = number * number;
// Check if the number is automorphic or not
while (number > 0) {
// If the last digit of the number and square are not equal, return false
if (number % 10 != square % 10) {
return false;
}
// Remove the last digit from the number and square
number /= 10;
// Remove the last digit from the square
square /= 10;
}
return true;
}
}