Subscribe Us

Automorphic Number in Java

Automorphic Number in Java

Java Program to Check Automorphic Number

In mathematics, an Automorphic number is a natural number
whose square "ends" in the same digits as the number itself.

Example:
Given Number : 25
It is an Automorphic Number
Square of a Give Number : 25*25=625
Since :
square Ends in the same digits as the number itself(Given Number).
Example:
Given Number : 12
It is Not an Automorphic Number
Square of a Give Number : 12*12=144
Since :
square Not Ends in the same digits as the number itself(Given Number).

Algorithm To check if a number is an Automorphic Number or Not in Java
you can use the following algorithm
  1. Read the input number.
  2. Square the input number and store it in a variable square.
  3. Initialize a variable count to 0.
  4. While num is greater than or equal to 0 (Zero), perform the following steps:
    • Extract the last digits digits from the num by using the modulo operator (%) and store it in a variable num.
    • Increment count by 1.
  5. Check if remainder is equal to the input number.
  6. If they are equal, the number is an Automorphic Number. Otherwise, it is not.
  7. Print the result.

In this algorithm, the input number is squared and the number of digits in the square is
calculated. Then, the algorithm extracts the last digits digits from the square using the
modulo operator and compares it with the input number. If they are equal, the number is considered an Automorphic Number.

Please note that this implementation assumes that the input number is a non-negative integer.
You may need to add additional checks for negative numbers or other cases depending on your
requirements.

Here is an example implementation in Java

Source Code

// Java Program to Check Automorphic Number 
import java.util.*;
public class Automorphic
  {
    public static void main(String args[])
      {
          Scanner in = new Scanner(System.in);
          int num , Square , count=0,ncopy;
          
          System.out.print("Enter a Number To Check : ");
          num = in.nextInt();
          Square=num*num;
          ncopy=num;
          while( num!=0 )
            {
                num=num/10;
                count++;
            }
          if( Square%Math.pow(10,count)==ncopy)
            System.out.print("It is an Automorphic Number . ");
          else
            System.out.print("It is Not an Automorphic Number . ");
      }
  }

Output

Sample Input :
Enter a Number To Check : 25
Sample Output:
It is an Automorphic Number

Sample Input :
Enter a Number To Check : 6
Sample Output:
It is an Automorphic Number

Sample Input :
Enter a Number To Check : 7
Sample Output:
It is Not an Automorphic Number