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**

- 1 [
**1**^{2}=**1**] - 5 [
**5**^{2}= 2**5**] - 6 [
**6**^{2}= 3**6**] - 25 [
**25**^{2}= 6**25**] - 76 [
**76**^{2}= 57**76**]…. 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"); } } }

**OUTPUT of the above code**

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 of`AutomorphicNumber`

class first the number to be checked is input from console using`Scanner.nextInt()`

, and stored in an`int`

variable named`inputNo`

. - Then the number and its square are both converted to
`String`

format using`String.valueOf()`

method. - Lastly, the condition for
`inputNo`

being an automorphic number is checked by finding**whether the string containing square of inputNo**. If true, then the number is determined to be an automorphic number, else, the number is NOT an automorphic number.*ends with*inputNo string value - A few runs of the program are shown in output with input numbers checked being
`5`

,`9`

and`76`

.

Java program for numerical 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"); } } }

**OUTPUT of the above code**

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 of`AutomorphicNum`

class first the number to be checked is input from console using`Scanner.nextInt()`

, and stored in an`int`

variable named`inputNo`

. - Then the number of digits in the
`inputNo`

is determined using a while loop. In while loop a copy of`inputNo`

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 named`noOfDigits`

, is the number of digits in the`inputNo`

. - Next, the square of
`inputNo`

is applied the modulus(% or remainder) operator with the divisor being. The power-of is done using`10`

raised to the power`noOfDigits`

`Math.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 named`lastNDigits`

. - We now have the
`inputNo`

and the extracted number(`lastNDigits`

) from squared`inputNo`

. To check for`inputNo`

being automorphic we simply check whether`inputNo`

equals`lastNDigits`

or not. - A few runs of the program are shown in output with input numbers checked being
`5`

,`9`

and`76`

.

Copyright © 2014-2021 JavaBrahman.com, all rights reserved.