Skip to content

Commit f3bf250

Browse files
authored
Automorphic Number (TheAlgorithms#2343)
1 parent b5f4ab2 commit f3bf250

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

Maths/AutomorphicNumber.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* A number is said to be an Automorphic, if it is present in the last digit(s) of its square.
3+
* Example- Let the number be 25, its square is 625.
4+
* Since, 25(The input number) is present in the last two digits of its square(625),
5+
* it is an Automorphic Number.
6+
*/
7+
import java.io.*;
8+
9+
public class AutomorphicNumber
10+
{
11+
//returns True if the number is a Automorphic number and False if it is not an Automorphic number
12+
public static boolean isAutomorphic(int n)
13+
{
14+
int m, c, r, p, k; c = 0;
15+
/** m = Temporary variable to store a copy of the number entered by the user.
16+
* n = The number entered by the user
17+
* c = Count the digits of the number entered by user.
18+
* p = To calculate the square of the number.
19+
* k = Support variable to count the digits of the number
20+
*/
21+
double s;
22+
m = n;
23+
p = m * m; //Calculating square of the number
24+
do
25+
{
26+
k = n / 10;
27+
c = c + 1; //Counting the digits of the number entered by user.
28+
n = k;
29+
}
30+
while(n != 0);
31+
s = Math.pow(10, c);
32+
r = p %(int)s;
33+
if(m == r) //Checking if the original number entered is present at the end of the square
34+
{
35+
return true;
36+
}
37+
else
38+
{
39+
return false;
40+
}
41+
}
42+
43+
/** Method to check if number is Automorphic Number or Not
44+
* 1) Input - Enter a Number: 25
45+
* Output - It is an Automorphic Number.
46+
* 2) Input - Enter a Number: 7
47+
* Output - It is not an Automorphic Number.
48+
*/
49+
public static void main(String args[]) throws IOException
50+
{
51+
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
52+
System.out.println("Enter a Number: ");
53+
int n=Integer.parseInt(br.readLine());
54+
if(isAutomorphic(n))
55+
{
56+
System.out.println("It is an Automorphic Number.");
57+
}
58+
else
59+
{
60+
System.out.println("It is not an Automorphic Number.");
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)