Automorphic number in Java
In this post, you will learn to check whether a given number is an automorphic number or not using the Java programming language.
An automorphic number is an integer whose square ends with the given integer itself. For example- 5, 6 are automorphic numbers as the last digit of the square represent the number itself. Similarly, 25, 76, and 376 are automorphic numbers as the last two digits of the square represent the number itself. The given image demonstrates this-
Steps to Check Automorphic Number in Java
These are the steps to check the automorphic number-
- Read a number (num) and store it in a variable.
- Find the square of the given number and store it in a variable.
- Find the last digit(s) of the square.
- Compare the last digit(s) of the (sqr) with num.
- If they are not equal, the given number is not an automorphic number, else not.
Java program to check automorphic number
In the given Java program, we have checked whether the number is automorphic or not using the while loop.
public class AutomorphicNumberProgram
{
// defining static method to checks
// whether the number is automorphic or not
static boolean checkAutomorphic(int num)
{
// calculating the square of the specified number
int square = num * num;
while (num > 0)
{
if (num % 10 != square % 10)
return false;
num = num/10;
square = square/10;
}
return true;
}
//Driver code
public static void main(String args[])
{
// Calling the defined method
// to check Automorphic number
System.out.println(checkAutomorphic(25) ? "Automorphic" : "Not Automorphic");
System.out.println(checkAutomorphic(33) ? "Automorphic" : "Not Automorphic");
System.out.println(checkAutomorphic(376) ? "Automorphic" : "Not Automorphic");
System.out.println(checkAutomorphic(76) ? "Automorphic" : "Not Automorphic");
}
}
Output of the above code:
Automorphic
Not Automorphic
Automorphic
Automorphic
Java program to check automorphic number in interval
In the given example, we have found all the automorphic numbers between the interval 1 to 1000.
// Java program to check automorphic numbers
// between interval
public class AutomorphicNumberExample3
{
// defining static method to checks
// whether the number is automorphic or not
private static boolean checkAutomorphic(int num)
{
int count=0;
// calculating the square of the specified number
int square = num*num;
// initializing temp variable and
// copying the num into temp
int temp = num;
while(temp>0)
{
count++;
//removes last digit of the variable num
temp=temp/10;
}
// getting the last digit of the variable square
int lastdigit = (int) (square%(Math.pow(10, count)));
//compare the last digit with num
return num == lastdigit;
}
public static void main(String args[])
{
int start, end;
start = 1;
end = 1000;
System.out.println("Automorphic numbers between "+start+" and "+end+" are: ");
for(int i=start; i<=end; i++)
{
// Calling the defined method
// to check Automorphic number
if(checkAutomorphic(i))
//prints the automorphic number
System.out.print(i+" ");
}
}
}
Output of the above code:
Automorphic numbers between 1 and 1000 are:
1 5 6 25 76 376 625
Related Articles
Number pattern programs in JavaJava program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number