Automorphic Number Program in Java
We will learn about automorphic numbers through examples in this article, and we'll also make Java programmes that can determine whether a specific number was automorphic or not.
What is an automorphic number?
Only when the square of a given number ends in that same number itself is a number considered to be automorphic. For instance, the numbers 25, and 76 are automorphic since their squares, 625 and 5776, respectively, contain the number itself in its final two digits. 5, 6, 36, 890625, and other such numbers are automorphic as well.
How to find automorphic number?
Observe the instructions below:
- Read a user-provided number (num).
- A variable should be used to hold the square of a given value (square).
- Find the square's final digit or digits.
- The variable's final digit(s) are compared to num.
- The provided integer isn't an automorphic number if they are not equal.
- Proceed to the following step if they're the same.
- The very last digit of a given number, num, must be removed.
- Till the specified number equals 0, repeat steps 4 through 6.
Java Automorphic Number Program
Automorphic1.java
public class Automorphic1
{
// A static method specified by the user that determines
// whether or not the integer is automorphic
static boolean Atuomorphicrnot(int int)
{
// determines the provided number's square
int sqr = int * int;
// the digits are compared till the number equals 0.
while (int > 0)
{
// finding the final digit of the field int and comparing
// it to square
if (int % 10 != sqr % 10)
// if the digits are not equal, returns false.
return false;
// By reducing int & square by 10, we may minimise them.
int = int / 10;
square = sqr/10;
}
return true;
}
// Main method starts here
public static void main(String args[])
{
// running the procedure with the number to check and
// printing the outcome appropriately
System.out.println(Atuomorphicrnot(76) ? "it is Automorphic" : "it is Not Automorphic");
System.out.println(Atuomorphicrnot(13) ? "it is Automorphic" : "it is Not Automorphic");
}
}
Output:
it is Automorphic
it is Not Automorphic
Checking the number to see if it is automorphic requires another logic.
Automorphic2.java
import java.util.Scanner;
public class Automorphic2
{
public static void main(String args[])
{
Scanner scr = new Scanner(System.in);
System.out.print("Enter a number to check: ");
// reading a user-provided number
int int1 = scr.nextInt();
int c = 0;
// finds the provided number's square
int sqr = int1*int1;
// the variable int is copied into t
int t = int1;
// until the condition is false, repeat the iteration over the variable int.
while(t > 0)
{
c++;
// removes the variable's last digit.
t = t / 10;
}
// determines the variable square's final digit.
int ldigit = (int) (sqr % (Math.pow(10, c)));
// int is compared to the variable square's last digit.
if(int == ldigit)
System.out.println(int1 + " it is the automorphic number.");
else
System.out.println(int1 + " it is not the automorphic number.");
}
}
Output:
Enter a number to check: 625
625 it is the automorphic number.
Let's write a Java programme that finds every automorphic number that falls within a given range.
Automorphic3.java
import java.util.Scanner;
public class Automorphic3
{
// a static method specified by the user that determines whether or not the integer is automorphic
private static boolean isAutomorphic(int n)
{
int c = 0;
// finds the provided number's square
int sqr = n*n;
// the variable n is copied into t
int t = n;
// until the condition is false, repeat the iteration over the variable n.
while(t > 0)
{
c++;
// removes the variable's last digit.
t = t / 10;
}
// determines the variable square's final digit.
int ldig = (int) (sqr % (Math.pow(10, c)));
// comparing the final digit to number
return n == ldig;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int s, e;
System.out.print("Enter the starting value: ");
s = sc.nextInt();
System.out.print("Enter the ending value: ");
e = sc.nextInt();
System.out.println("Automorphic numbers between " + s + " and " + e + " are: ");
// The for loop begins with the initial value and continues to run until the condition is false.
for(int i=s; i<=e; i++)
{
// calling the custom method
if(isAutomorphic(i))
// if the number is automorphic, prints it.
System.out.print(i + " ");
}
}
}
Output:
Enter the starting value: 1
Enter the ending value: 700
Automorphic numbers between 1 and 700 are:
1 5 6 25 76 376 625