Java Program to check for Automorphic Number
This tutorial shows how to write Java program to check whether given number is automorphic number(or circular number) or not. Tutorial first explains what is an automorphic number. It then provides two Java programs to check for automorphic number. First program checks for automorphic number after converting the number and its square to string format. The second Java program relies on division\remainder based logic for automorphic number determination.
What is an automorphic number
An automorphic number(aka circular number) is a number which when squared ends with the same digits as that in the original number. Let us take a few examples of automorphic numbers to understand better.
Examples of automorphic numbers
OUTPUT of the above code
Explanation of the code
OUTPUT of the above code
Explanation of the code
Examples of automorphic numbers
- 1 [ 12 = 1 ]
- 5 [ 52 = 25 ]
- 6 [ 62 = 36 ]
- 25 [ 252 = 625 ]
- 76 [ 762 = 5776 ].... and so on
Java program using string-based logic for automorphic number check
package com.javabrahman.generaljava;
import java.util.Scanner;
public class AutomorphicNumber {
public static void main(String args[]) {
//Input number to be tested
System.out.print("Enter number to be tested:");
Scanner scanner = new Scanner(System.in);
int inputNo = scanner.nextInt();
//Convert the input number and its square to string
String inputNoStr = String.valueOf(inputNo);
String squaredrStr = String.valueOf(inputNo * inputNo);
//Check if the squared no's string ends with input no
if (squaredrStr.endsWith(inputNoStr)) {
System.out.println(inputNo + " is an automorphic number");
} else {
System.out.println(inputNo + " is NOT an automorphic number");
}
}
}
Enter number to be tested: 5 5 is an automorphic number
Enter number to be tested: 9 9 is NOT an automorphic number
Enter number to be tested: 76 76 is an automorphic number
- In the
main()
method ofAutomorphicNumber
class first the number to be checked is input from console usingScanner.nextInt()
, and stored in anint
variable namedinputNo
. - Then the number and its square are both converted to
String
format usingString.valueOf()
method. - Lastly, the condition for
inputNo
being an automorphic number is checked by finding whether the string containing square of inputNo ends with inputNo string value. If true, then the number is determined to be an automorphic number, else, the number is NOT an automorphic number. - A few runs of the program are shown in output with input numbers checked being
5
,9
and76
.
Java program for division/remainder logic based automorphic number determination
package com.javabrahman.generaljava;
import java.util.Scanner;
public class AutomorphicNum {
public static void main(String args[]) {
//Input number to be tested
System.out.print("Enter number to be tested:");
Scanner scanner = new Scanner(System.in);
int inputNo = scanner.nextInt();
//Find the number of digits in input number
int noOfDigits=0;
int copyOfInput=inputNo;
while(copyOfInput!=0){
copyOfInput=copyOfInput/10;
noOfDigits++;
}
//Square the inputNo and get last 'n' digits of squared value
//where 'n' = no of digits in inputNo = noOfDigits
int lastNDigits=(int)((inputNo*inputNo)%(Math.pow(10,noOfDigits)));
//Check if lastNDigits equals inputNo
if (lastNDigits==inputNo) {
System.out.println(inputNo + " is an automorphic number");
} else {
System.out.println(inputNo + " is NOT an automorphic number");
}
}
}
Enter number to be tested: 5 5 is an automorphic number
Enter number to be tested: 9 9 is NOT an automorphic number
Enter number to be tested: 76 76 is an automorphic number
- In the
main()
method ofAutomorphicNum
class first the number to be checked is input from console usingScanner.nextInt()
, and stored in anint
variable namedinputNo
. - Then the number of digits in the
inputNo
is determined using a while loop. In while loop a copy ofinputNo
is progressively divided by 10, and the count of iterations required for the number to become 0 is determined. This count of iterations, stored in a variable namednoOfDigits
, is the number of digits in theinputNo
. - Next, the square of
inputNo
is applied the modulus(% or remainder) operator with the divisor being10
raised to the powernoOfDigits
. The power-of is done usingMath.pow()
method. - What the remainder operation does is that it extracts as a remainder exactly the same number of digits as that in
inputNo
. This extracted number is then stored in a variable namedlastNDigits
. - We now have the
inputNo
and the extracted number(lastNDigits
) from squaredinputNo
. To check forinputNo
being automorphic we simply check whetherinputNo
equalslastNDigits
or not. - A few runs of the program are shown in output with input numbers checked being
5
,9
and76
.