In this tutorial we will learn about what is automorphic number in java and write Java program for automorphic number in java.
What is automorphic number in java
A number is called an automorphic number if and only if the square of the given number ends with the same number itself.
For example 5 , 6, 25 are automorphic numbers because their square is 15 , 36 and 625 respectively and last digit are same as number itself .
Java program for automorphic number
In this automorphic number program in java we take input from user to check given number is automorphic or not with the help of scanner . We use following steps to check given number is automorphic number or not .
- take input from user num
- calculate square sqr of num
- count digit of num
- now take last count digit of square sqr of num and compare with num
- if last digit are equal to num then given number is automorphic number .
public class AutomorphicNumber {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a number");
int num = in.nextInt();
int count=0, sqr = num*num;
int temp =num;
//count digit of number
while(temp>0){
count++;
temp=temp/10;
}
//taking last count digit of sqr of num
int lastSquareDigits = (int) (sqr%(Math.pow(10,count)));
if(num == lastSquareDigits)
System.out.println("Given number is Automorphic number");
else
System.out.println("Given number is not an Automorphic number");
}
}
Output :
Enter a number
5
Given number is Automorphic number
Now we will write one more java program to check given number is automorphic number using different logic . In this program we write a function to check automorphic number and steps as following
- take input from user
- call function
isAutomorphicNumber(int num)
- calculate square sqrt of num
- Now iterate while loop till num is > 0
- In loop we compare last digit of num and sqrt
- if last digit does not match then we return false
- if last digit is equal then num and sqrt divide by 10 and assign result in num and sqrt respectively .
- if num matches with last digit of sqrt then it will retun true at the end .
import java.util.Scanner;
public class AutomorphicNumber2 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a number");
int num = in.nextInt();
if(isAutomorphicNumber(num))
System.out.println("Given number is Automorphic number");
else
System.out.println("Given number is not an Automorphic number");
}
public static boolean isAutomorphicNumber(int num){
int sqrt = num * num;
while ( num > 0) {
//compare last digit of num and sqrt
if ( num %10 != sqrt%10 ){
return false ;
}
num = num /10;
sqrt = sqrt/10;
}
return true;
}
}
Output :
Enter a number
6
Given number is Automorphic number
Java Program to Find all Automorphic Numbers in the Interval
Here we write java program to find all automorphic number in given range .
import java.util.Scanner;
public class AutomorphicNumberForRange {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a lower range value");
int low = in.nextInt();
System.out.println("Enter a upper range value");
int upper = in.nextInt();
for (int i = low; i <= upper; i++) {
if(isAutomorphicNumber(i))
System.out.println( i +" ");
}
}
public static boolean isAutomorphicNumber(int num){
int count=0, sqr = num*num;
int temp =num;
//count digit of number
while(temp>0){
count++;
temp=temp/10;
}
//taking last count digit of sqr of num
int lastSquareDigits = (int) (sqr%(Math.pow(10,count)));
return num == lastSquareDigits;
}
}
Output:
Enter a lower range value
1
Enter a upper range value
100
1
5
6
25
76
In this tutorial we have written java program for automorphic number using different logic . you can learn java coding program for interview preparation .