-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathAutomorphic_Number.java
27 lines (23 loc) · 1.16 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
/**
Automorphic number is a number which of squaring ends with the same number.
Example : (25)^2 = 625, 625 ends with 25 so its an Automorphic number
*/
import java.util.*; // import the classes of the util package
public class Automorphic_Number {
public static boolean isAutomorphic(int n) { // this boolean method checks if a number is automorphic or not
String st = (n*n) + ""; // this string stores the square of the number
String s = n + ""; // this string stores the number and converts it explicitly
return st.endsWith(s); // checking if the square of the number ends with the number itself
}
public static void main(String[] args) { // this is the main() method
Scanner in = new Scanner(System.in); // scanner class object creation
System.out.println("Enter a number : ");
int n = in.nextInt(); // taking a number input from the user
// checking if the number is automorphic or by invoking the isAutomorphic method!
if(isAutomorphic(n))
System.out.println("The number is a automorphic number.");
else
System.out.println("The number is not automorphic number.");
in.close(); // preventing resource leak!!
}
}