Table of contents
1.
Introduction
2.
What is an automorphic number?
3.
How to find automorphic numbers?
4.
Java Automorphic Number Program
4.1.
Java
5.
Examples
5.1.
Example 1
5.2.
Example 2
5.3.
Example 3
6.
Frequently Asked Questions
6.1.
Can negative numbers be automorphic?
6.2.
Are there any even automorphic numbers?
6.3.
Is every number that ends with 5 an automorphic number?
7.
Conclusion
Last Updated: Jul 22, 2024
Easy

Automorphic Number in Java

Author Rinki Deka
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

An automorphic number is a special type of number that, when squared, contains the original number at the end of the result. In other words, the square of an automorphic number ends with the number itself. For example, 25 is an automorphic number because its square (25^2 = 625) ends with 25. Automorphic numbers have interesting properties & can be identified using simple mathematical calculations. 

Automorphic Number in Java

In this article, we will learn what automorphic numbers are, how to find them, & see how to implement a Java program to check if a given number is automorphic or not. 

What is an automorphic number?

An automorphic number is a number that, when multiplied by itself (squared), results in a product that ends with the original number. In mathematical terms, if a number "n" is automorphic, then the square of "n" (n^2) will have "n" as its last digits.

For instance, let's consider the number 5. When we square 5, we get:

5^2 = 25


Notice that the result (25) ends with the original number (5). Therefore, 5 is an automorphic number.

Another example is the number 76. Let's square it:

76^2 = 5,776


Again, we can see that the result (5,776) ends with the original number (76), confirming that 76 is also an automorphic number.

Automorphic numbers can have any number of digits, but the key characteristic is that their square always ends with the original number itself. This property makes automorphic numbers interesting from a mathematical perspective & has led to various studies & investigations into their properties & patterns.

How to find automorphic numbers?


To determine whether a number is automorphic or not, we need to follow a simple procedure:
 

1. Square the number (multiply it by itself).
 

2. Check if the resulting square ends with the original number.


Let's take an example to show this process. Consider the number 376.

Step 1: Square the number.

376^2 = 141,376


Step 2: Check if the square ends with the original number.

The square (141,376) ends with 376, which is the original number.


Therefore, 376 is an automorphic number.

Let’s put this process into a step-by-step algorithm:

1. Take a number as input.
 

2. Calculate the square of the number.
 

3. Convert the original number to a string & store its length.
 

4. Convert the squared result to a string.
 

5. Check if the squared result ends with the original number by comparing the last digits of the squared result with the original number.
 

   - If the last digits of the squared result match the original number, it is an automorphic number.

   - If the last digits of the squared result do not match the original number, it is not an automorphic number.

Java Automorphic Number Program

Now that we understand what automorphic numbers are & how to find them, let's implement a Java program to check if a given number is automorphic or not :

  • Java

Java

import java.util.Scanner;

public class AutomorphicNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();

if (isAutomorphic(number)) {
System.out.println(number + " is an automorphic number.");
} else {
System.out.println(number + " is not an automorphic number.");
}
}

public static boolean isAutomorphic(int number) {
int square = number * number;
String numberStr = String.valueOf(number);
String squareStr = String.valueOf(square);

return squareStr.endsWith(numberStr);
}
}
You can also try this code with Online Java Compiler
Run Code


Output

Enter a number: 6
6 is an automorphic number.


In this program : 

1. We import the `Scanner` class to take input from the user.
 

2. In the `main` method, we create a `Scanner` object & prompt the user to enter a number. We store the input in the `number` variable.
 

3. We call the `isAutomorphic` method, passing the `number` as an argument. This method will return `true` if the number is automorphic & `false` otherwise.
 

4. Based on the result of `isAutomorphic`, we print whether the number is an automorphic number or not.
 

5. In the `isAutomorphic` method, we first calculate the square of the number by multiplying it by itself & store it in the `square` variable.
 

6. We convert both the original number & the square to strings using `String.valueOf()` & store them in `numberStr` & `squareStr`, respectively.
 

7. Finally, we use the `endsWith()` method to check if `squareStr` ends with `numberStr`. If it does, we return `true`, indicating that the number is automorphic. Otherwise, we return `false`.


This program provides a simple & efficient way to check if a given number is automorphic or not. The `isAutomorphic` method can be reused for any number, making it a useful utility function.

Examples

Let's look at a few examples to better understand how the Java program works:

Example 1

Enter a number: 25

Output: 25 is an automorphic number.


Explanation

25^2 = 625

The square of 25 (625) ends with 25, so it is an automorphic number.

Example 2

Enter a number: 12

Output: 12 is not an automorphic number.


Explanation

12^2 = 144

The square of 12 (144) does not end with 12, so it is not an automorphic number.

Example 3

Enter a number: 890625

Output: 890625 is an automorphic number.


Explanation

890625^2 = 793212890625

The square of 890625 (793212890625) ends with 890625, so it is an automorphic number.


These examples demonstrate how the Java program correctly identifies whether a given number is automorphic or not. The program works for numbers of various lengths & efficiently checks the automorphic property.

Frequently Asked Questions

Can negative numbers be automorphic?

No, negative numbers cannot be automorphic because their squares are always positive.

Are there any even automorphic numbers?

Yes, there are even automorphic numbers, such as 0, 6, 76, 376, & 90625.

Is every number that ends with 5 an automorphic number?

No, not every number ending with 5 is automorphic. For example, 15 & 35 are not automorphic.

Conclusion

In this article, we learned about automorphic numbers, which are numbers whose squares end with the original number itself. We discussed about the concept, learned how to identify automorphic numbers, & implemented a Java program to check if a given number is automorphic. 

You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass