Write A Program To Check An Integer Number Is An Automorphic Number Or Not In The Java
An automorphic number is an integer whose square ends with the number itself. For example, 5 is an automorphic number because 5*5 = 25, and 25 ends with 5. In this article, you will learn how to write a Java program to determine if a given integer is an automorphic number.
Problem Statement
The task is to determine whether a given non-negative integer is an automorphic number. This involves calculating the square of the number and then checking if the last digits of the square match the original number.
Example
If the input number is 6, its square is 36. Since 36 ends with 6, the number 6 is automorphic.
Background & Knowledge Prerequisites
To understand this article, you should have a basic understanding of:
- Java programming fundamentals (variables, data types, operators).
- Input/output operations in Java.
- Conditional statements (
if-else). - Mathematical operations like squaring a number and finding its last digits (modulo operator).
Use Cases or Case Studies
- Educational Tools: Demonstrating number properties in mathematics or programming courses.
- Number Theory Exploration: Used in recreational mathematics to identify special numbers.
- Basic Algorithm Practice: A good problem for practicing fundamental programming concepts and mathematical logic.
- Data Validation: In some niche applications, checking for specific number patterns might be part of a validation process.
Solution Approaches
We will explore one primary approach to solve this problem, focusing on mathematical operations.
Approach 1: Using Mathematical Operations
This approach involves calculating the square of the number and then comparing the original number with the last digits of its square. We can extract the last digits using the modulo operator and powers of 10.
One-line summary: Calculate the square, then use modulo arithmetic to compare the original number with the end digits of its square.
// Automorphic Number Checker
import java.util.Scanner;
// Main class containing the entry point of the program
public class Main {
public static void main(String[] args) {
// Step 1: Create a Scanner object to read input from the user
Scanner scanner = new Scanner(System.in);
// Step 2: Prompt the user to enter a number
System.out.print("Enter an integer: ");
int number = scanner.nextInt();
// Step 3: Calculate the square of the number
long square = (long) number * number; // Use long to prevent overflow for larger numbers
// Step 4: Determine the number of digits in the original number
// This is needed to extract the correct number of last digits from the square
int tempNumber = number;
int numberOfDigits = 0;
if (tempNumber == 0) { // Handle the case for number 0
numberOfDigits = 1;
} else {
while (tempNumber > 0) {
tempNumber /= 10;
numberOfDigits++;
}
}
// Step 5: Calculate the divisor to extract the last 'numberOfDigits' from the square
// For example, if number is 5 (1 digit), divisor is 10^1 = 10
// If number is 25 (2 digits), divisor is 10^2 = 100
long divisor = 1;
for (int i = 0; i < numberOfDigits; i++) {
divisor *= 10;
}
// Step 6: Extract the last digits of the square
long lastDigitsOfSquare = square % divisor;
// Step 7: Compare the original number with the extracted last digits
if (number == lastDigitsOfSquare) {
System.out.println(number + " is an automorphic number.");
} else {
System.out.println(number + " is not an automorphic number.");
}
// Step 8: Close the scanner
scanner.close();
}
}
Sample Output:
Enter an integer: 5
5 is an automorphic number.
Enter an integer: 25
25 is an automorphic number.
Enter an integer: 7
7 is not an automorphic number.
Stepwise explanation for clarity:
- Input: The program first takes an integer input from the user.
- Calculate Square: It calculates the square of the input number. We use
longfor the square to accommodate larger numbers and prevent potential integer overflow. - Count Digits: It determines the number of digits in the original input number. This is crucial because we need to compare the original number with *exactly* that many last digits of its square. For example, if the number is 5, we compare it with the last *one* digit of 25. If the number is 25, we compare it with the last *two* digits of 625.
- Calculate Divisor: Based on the number of digits, a
divisor(a power of 10) is calculated. This divisor will be used with the modulo operator to extract the required number of last digits from the square. For instance, if there arendigits, the divisor will be 10^n. - Extract Last Digits: The modulo operator (
%) is used with the calculateddivisorto get the last digits of the square.square % divisorgives the remainder whensquareis divided bydivisor, which corresponds to its lastnumberOfDigitsdigits. - Compare: Finally, the original number is compared with the extracted last digits of its square. If they are equal, the number is automorphic; otherwise, it is not.
- Close Scanner: The
Scannerobject is closed to release system resources.
Conclusion
Checking if a number is automorphic involves a straightforward process of squaring the number and then comparing the original number with the trailing digits of its square. The modulo operator and careful handling of digit counts are key to implementing this logic effectively in Java.
Summary
- An automorphic number's square ends with the number itself.
- The solution involves calculating the square of the input number.
- Determine the number of digits in the original number to know how many trailing digits to check in the square.
- Use the modulo operator (
%) with a power of 10 to extract the relevant last digits from the square. - Compare the original number with these extracted last digits to determine if it's automorphic.