An Automorphic number is a number whose square ends with the same digits as the original number. E.g – 25, 76, 376 etc.

Automorphic numbers

Steps to Check Automorphic Number in Java

  1. Take a number as input (num).
  2. Square the number (sqr).
  3. Count the number of digits of (num) using while loop (c).
  4. Compare the last (c) digits of (sqr) with the (num).
  5. If they are equal then the number is Automorphic else not.

Check Whether a number is an Automorphic Number in Java

Output

Java Program to Find all Automorphic Numbers in the Interval

We know how to check whether a number is Automorphic or not using the above java program. Now, to find all Automorphic numbers in the given range, we need to define a function which returns true or false depending on whether the passed number as a parameter is automorphic or not. We will check for all the values in the given interval by passing it to the function one by one using loop.

Output

Any Doubts comment below.

This Post Has 2 Comments

  1. Soumojeet Ro Y

    Can u please explain the process I can’t understand 😞🙏😔

    1. Adarsh Kumar

      To check if a number is automorphic or not, we first need to count the number of digits of the input number. For ex: 76 has 2 digits.
      To count the digits we divide the number by 10 and at the same time increment ‘c’ which was initially 0. After some iterations the number will reduce to 0 and cannot be divided by 10 (76/10=7 -> 7/10=0 ). At this point the value of c will be the number of digits in the input number.
      Now we know the count (c) of digits of the input number, we will use it to extract last ‘c’ digits from the ‘square of the input number’.
      To extract last ‘c’ digits we will use sqr%(Math.pow(10,c).

      Suppose 76 is the input, its square is 5776. Using while loop we got c=2. To extract last 2 digits of 5776 we will divide it by 100 (math.pow(10,2)). The remainder will get us last two digits(5776%100=76).

      Now compare the last two digits with the input value i.e 76 == 76

      True so automorphic number.

Leave a Reply