Read Also: Ugly Number in Java
What is Automorphic Number
According to Wikipedia,In mathematics, Automorphic numbers are those natural numbers whose square "ends" in the same digits as the number itself.
Examples:
Number to check : 25
Square of the number : 625
Square ends with same digits as original number: Yes // 25 is an Automorphic number
Number to check : 376
Square of the number : 141376
Square ends with same digits as original number: Yes // 376 is an Automorphic number
Below are examples of numbers which are NOT Automorphic numbers
Number to check : 34
Square of the number : 1156
Square ends with same digits as original number: No // 34 is NOT an Automorphic number
Number to check : 147
Square of the number : 21609
Square ends with same digits as original number: No // 147 is NOT an Automorphic number
List of Automorphic numbers from 1 to 10000 are : 1, 5, 6, 25, 76, 376, 625, 9376
Read Also: Evil Number in Java
Java Program for Automorphic Number
import java.util.Scanner; public class JavaHungry { public static void main(String args[]) { System.out.println("Enter any number : "); Scanner scan = new Scanner(System.in); Integer inputNumber = scan.nextInt(); checkAutomorphicNumber(inputNumber); } public static void checkAutomorphicNumber(Integer inputNumber) { // Calculating square of the inputNumber int sq = inputNumber * inputNumber; // Converting inputNumber to String String number = Integer.toString(inputNumber); // Converting sq to String String square = Integer.toString(sq); /* Using String endsWith() function to check if square ends with digits of inputNumber */ if(square.endsWith(number)) System.out.println(inputNumber + " is an Automorphic Number"); else System.out.println(inputNumber + " is NOT an Automorphic Number"); } }
Output:
Enter any number :
8 is NOT an Automorphic Number
Algorithm for Automorphic Number
1. Calculate the square of the given inputNumber.2. Using String's class endsWith() function, check whether square of the inputNumber ends with the original number or not.
a. If true, then the inputNumber is an Automorphic number.
b. If false, then the inputNumber is not an Automorphic number.
Java Program for Automorphic Number without Using String Function
import java.util.Scanner; public class JavaHungry { public static void main(String args[]) { System.out.println("Enter any number : "); Scanner scan = new Scanner(System.in); Integer inputNumber = scan.nextInt(); checkAutomorphicNumber(inputNumber); } public static void checkAutomorphicNumber(Integer inputNumber) { // Calculating square of the inputNumber int square = inputNumber * inputNumber; int number = inputNumber; int count = 0; // Calculate the number of digits while(number > 0) { count++; number = number/10; } /* Finding the last digits of the square. if the inputNumber has 3 digits, then we will find the last 3 digits of the square using square % 1000 */ int squareLastDigits = square % (int)Math.pow(10, count); if(squareLastDigits == inputNumber) System.out.println(inputNumber + " is an Automorphic Number"); else System.out.println(inputNumber + " is NOT an Automorphic Number"); } }
Output:
Enter any number :
376 is an Automorphic Number
That's all for today, please mention in comments in case you have any questions related to the automorphic number in java with examples.
You may also like:
Perfect number program in Java (solution)
Special number program in Java (solution)
Bouncy number program in Java (answer)
Disarium number program in Java (answer)
Neon number program in Java (solution)
Happy number program in Java (answer)
Magic number program in Java (solution)
Pronic number program in Java (answer)
Duck number program in Java (answer)