Find automorphic number in java

In this post, we will see how to find automorphic number in java.

Automorphic number are those natural numbers whose square ends in same digits are number itself.

25 is automorphic number because:
square of 25 is 625 and 625 ends with same digits as original number 25.

76 is automorphic number because:
square of 76 is 5776 and 5776 ends with same digits as original number 76.

Find automorphic number using digits

Here is simple algorithm to find automorphic number using digits.

  • Take value of n from user using Scanner class
  • Find square of n sqrOfN
  • Count number of digits noOfDigits in n
  • Find last noOfDigits of sqrOfN and assign it to lastNDigitsOfSquare
  • Check if lastNDigitsOfSquare is equal to n
    • If yes, then number is automorphic
    • If no, then number is not automorphic

Output:

Enter a number
76
Square of n is: 5776
76 is automorphic number

Find automorphic number using String

We can also use String’s endsWith() method to find automorphic number using String.
Here is simple algorithm to find automorphic number using digits.

  • Take value of n from user using Scanner class and convert it to String numStr
  • Find square of n and convert it to String sqrNumStr
  • Check if sqrNumStr with numStr
    • If yes, then number is automorphic
    • If no, then number is not automorphic

Output:

Enter a number
25
Square of n is: 625
25 is automorphic number

Find automorphic number in range

We can simply use a for loop to iterate from minimum to maximum range interval and check if number is automorphic or not.

Output:

Enter minimum range: 5
Enter maximum range 100
The Automorphic numbers from 5 to 100 are:
5 6 25 76

That’s all about how to find automorphic number in java.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *