Google News
logo
What is an automorphic number? Java Program to Automorphic Number.
An automorphic number is a number whose square ends with the same digits as the number itself. For example, 5 is an automorphic number because 5^2 = 25, and the last digit of 25 is 5, which is the same as the original number .

Here's a Java program to check whether a number is an automorphic number or not :
Program :
import java.util.Scanner;

public class AutomorphicNumber {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = scanner.nextInt();
        int square = num * num;
        String str1 = Integer.toString(num);
        String str2 = Integer.toString(square);
        if (str2.endsWith(str1)) {
            System.out.println(num + " is an automorphic number.");
        } else {
            System.out.println(num + " is not an automorphic number.");
        }
        scanner.close();
    }
}
Output :
Enter a number: 5
5 is an automorphic number.
Enter a number: 23
23 is not an automorphic number.
In this program, we first take an input number from the user using the Scanner class. Then we calculate the square of the number and convert both the number and its square to strings using the toString() method.

Finally, we check if the square ends with the original number, and if it does, we print that the number is an automorphic number, otherwise, we print that it's not.