In this article, we’ll go over what automorphic numbers are and how to tell in Java if a given number is one. We’ll also talk about some programming use cases and applications for automorphic numbers.
What are Automorphic Number in Java?
When a number’s square is calculated and its rightmost digits are contrasted with the starting value, the number is said to be automorphic if it does not change. For instance, 5 is an automorphic number because 5 * 5 = 25 and 5 is the rightmost digit in 25, which is the same as the original number.
Understand the Concept of Java Program to Convert Number to Word, Here!
How to Determine Whether a Number is an Automorphic Number in Java?
In Java, you need to follow a straightforward algorithm to determine whether a number is an automorphic number. First, we compute the square of the number. Next, we extract the final digits of the square, matching the number of digits in the original number. In order to compare the extracted digits to the original number, we finish.
Methods to find Automorphic Number in Java
The various approaches to finding automorphic number in Java that we will discuss in this section are as follows:
Check whether the entered number is automorphic or not
This section will cover the provided code that determines whether or not a number is automorphic. Following is a more detailed explanation of the idea of an automorphic number and the strategy employed by the code:
import java.util.Scanner; public class AutomorphicNumber { public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.print("Enter a number to check: "); //reading a number from the user int num = in.nextInt(); int count=0; //determines the square of the given number int square = num*num; //copying the variable num into temp int temp = num; //iterate over the variable num until the condition become false while(temp>0) { count++; //removes last digit of the variable num temp=temp/10; } //determines the last digit of the variable square int lastDigit = (int) (square%(Math.pow(10, count))); //compare num with last digit of the variable square if(num == lastDigit) System.out.println(num+ " is an automorphic number."); else System.out.println(num+ " is not an automorphic number."); } }
Explanation of the code:
If an automorphic number exists or not is determined by the provided code. A number that has the same digit at the end of its square is said to be automorphic. The number 5 is an example of an automorphic number because 52 = 25 and 5 is the last digit in the number 25.
An integer is first read from the user by the code, which then stores it in the variable “num.” After that, it squares the value and stores the result in the variable “square”.
We then utilize the number of times the loop runs before the variable “temp” reaches zero to calculate the number of digits in the inputted number “num.” To preserve the initial value of “num” for future use, it is stored in the variable “temp.”
Then, we employ the formula square%(Math.pow(10, count)) to determine the final digit of the square. In this case, we store the number of digits in the entered number, “num,” in the variable “count.” This formula takes the square of the entered number and extracts the final “count” digits.
The code then performs a comparison between the extracted last “count” digits and the original “num” number. If they are identical, the program displays a message stating that the number is an automorphic number.If they are not identical, the program shows a message indicating that the number is not automorphic.
Check out our blog on Reverse a Number in Java here!
Output:
Enter a number to check: 45612
45612 is not an automorphic number.
Automorphic number within the specified range
The below Java code actively determines automorphic numbers within a user-defined range. The program prompts the user to input the starting and ending values for the range.
import java.util.Scanner; public class AutomorphicNumber { //user-defined static method that checks whether the number is automorphic or not private static boolean isAutomorphic(int num) { int count=0; //determines the square of the given number int square = num*num; //copying the variable num into temp int temp = num; //iterate over the variable num until the condition become false while(temp>0) { count++; //removes last digit of the variable num temp=temp/10; } //determines 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[]) { Scanner in = new Scanner(System.in); int start, end; System.out.print("Enter the starting value: "); start = in.nextInt(); System.out.print("Enter the ending value: "); end = in.nextInt(); System.out.println("Automorphic numbers between "+start+" and "+end+" are: "); //the for loop starts from the starting value and execute until the condition becomes false for(int i=start; i<=end; i++) { //calling the user-defined method if(isAutomorphic(i)) //prints the number if it is automorphic System.out.print(i+" "); } } }
Explanation of the code:
In this method, the program takes two input values from the user: starting and ending values for the range. It then checks each number within the range using the is Automorphic method to determine if it is an automorphic number or not.
The Automorphic method takes an integer number as input and returns a boolean value. This method first calculates the square of the input number using the num*num expression. It then counts the number of digits in the input number using a while loop and a temporary variable. This is done to determine the number of digits that need to be compared in the automorphic number.
Next, it uses the modulo operator and the Math.pow method to determine the last digit of the square. Then, it compares the result to the original input number, and if they are the same, it returns true, indicating that the number is an automorphic number.
The main method actively takes input values from the user and prints the automorphic numbers within the given range. It does this by calling the isAutomorphic method for each number within the range and printing the numbers that return true.
Check out Python Interview Questions and Answers, Now!
Output:
Enter the starting value: 20
Enter the ending value: 450
Automorphic numbers between 20 and 450 are: 25 76 376
Advantages and Applications of Automorphic Number in Java
Advantages of Automorphic Number in Java:
- In modular arithmetic, automorphic numbers actively reduce computation time as they enable the quick calculation of a number’s final few digits.
- Encryption algorithms actively utilize automorphic numbers to add an extra layer of security by making it harder for an attacker to decode the original message.
Applications of Automorphic Number in Java:
- In cryptography, automorphic numbers are particularly useful in the RSA encryption algorithm, which is widely used for safe online communication.
- In order to create magic squares, a type of mathematical puzzle where the numbers in each row, column, and diagonal add up to the same value, automorphic numbers are also used.
- Prime numbers are created using automorphic numbers, and prime numbers are used in a variety of mathematical contexts, including secure communication, data encryption, and cryptography.
In conclusion, automorphic number in Java have many uses in a variety of mathematical disciplines, such as prime number construction, magic squares, and cryptography.