In this post, we will see how to find automorphic number in java.
Table of Contents
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
ofsqrOfN
and assign it tolastNDigitsOfSquare
- Check if
lastNDigitsOfSquare
is equal ton
- If yes, then number is automorphic
- If no, then number is not automorphic
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
package org.arpit.java2blog; import java.util.Scanner; public class AutomorphicNumberMain { public static void main(String args[]){ Scanner in = new Scanner(System.in); System.out.println("Enter a number"); int n = in.nextInt(); boolean isAutomorphicNumber = isAutomorphicNumber(n); if(isAutomorphicNumber) { System.out.println(n +" is automorphic number"); } else { System.out.println(n +" is not automorphic number"); } } private static boolean isAutomorphicNumber(int n) { int noOfDigits=0; int sqrOfN = n * n; System.out.println("Square of n is: "+sqrOfN); int temp = n; //copying num // count digits of num while(temp>0){ noOfDigits++; temp=temp/10; } int lastNDigitsOfSquare = (int) (sqrOfN%(Math.pow(10,noOfDigits))); if(n == lastNDigitsOfSquare) return true; else return false; } } |
Output:
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 StringnumStr
- Find square of n and convert it to String
sqrNumStr
- Check if
sqrNumStr
withnumStr
- If yes, then number is automorphic
- If no, then number is not automorphic
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package org.arpit.java2blog; import java.util.Scanner; public class AutomorphicNumberUsingString { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter a number"); int n = in.nextInt(); boolean isAutomorphicNumber = isAutomorphicNumberUsingString(n); if(isAutomorphicNumber) { System.out.println(n +" is automorphic number"); } else { System.out.println(n +" is not automorphic number"); } } // Check automorphic number using String public static boolean isAutomorphicNumberUsingString(int n) { // convert numbers to string String numStr = Integer.toString(n); String sqrNumStr = Integer.toString(n*n); System.out.println("Square of n is: "+sqrNumStr); // check endWith if(sqrNumStr.endsWith(numStr)) return true; return false; } } |
Output:
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package org.arpit.java2blog; import java.util.Scanner; public class AutomorphicNumberRange { public static void main(String[] args) { int min = 0, max = 0; Scanner scanner = new Scanner(System.in); // Get min and max values System.out.print("Enter minimum range: "); min = scanner.nextInt(); System.out.print("Enter maximum range "); max = scanner.nextInt(); // check number System.out.println("The Automorphic numbers from "+ min+" to "+ max+" are: "); for(int i=min; i<=max; i++) { if(isAutomorphicNumber(i)) System.out.print(i+" "); } // close Scanner class object scanner.close(); } private static boolean isAutomorphicNumber(int n) { int noOfDigits=0; int sqrOfN = n * n; int temp = n; //copying num // count digits of num while(temp>0){ noOfDigits++; temp=temp/10; } int lastNDigitsOfSquare = (int) (sqrOfN%(Math.pow(10,noOfDigits))); if(n == lastNDigitsOfSquare) return true; else return false; } } |
Output:
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.