forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutomorphicNumber.java
More file actions
65 lines (62 loc) · 2.13 KB
/
AutomorphicNumber.java
File metadata and controls
65 lines (62 loc) · 2.13 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package Maths;
/**
* A number is said to be an Automorphic, if it is present in the last digit(s) of its square.
* Example- Let the number be 25, its square is 625.
* Since, 25(The input number) is present in the last two digits of its square(625),
* it is an Automorphic Number.
*/
import java.io.*;
public class AutomorphicNumber
{
//returns True if the number is a Automorphic number and False if it is not an Automorphic number
public static boolean isAutomorphic(int n)
{
int m, c, r, p, k; c = 0;
/** m = Temporary variable to store a copy of the number entered by the user.
* n = The number entered by the user
* c = Count the digits of the number entered by user.
* p = To calculate the square of the number.
* k = Support variable to count the digits of the number
*/
double s;
m = n;
p = m * m; //Calculating square of the number
do
{
k = n / 10;
c = c + 1; //Counting the digits of the number entered by user.
n = k;
}
while(n != 0);
s = Math.pow(10, c);
r = p %(int)s;
if(m == r) //Checking if the original number entered is present at the end of the square
{
return true;
}
else
{
return false;
}
}
/** Method to check if number is Automorphic Number or Not
* 1) Input - Enter a Number: 25
* Output - It is an Automorphic Number.
* 2) Input - Enter a Number: 7
* Output - It is not an Automorphic Number.
*/
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a Number: ");
int n=Integer.parseInt(br.readLine());
if(isAutomorphic(n))
{
System.out.println("It is an Automorphic Number.");
}
else
{
System.out.println("It is not an Automorphic Number.");
}
}
}