Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

Java Program to Check Automorphic Number

Posted in Java Tutorial
Updated on Oct 30, 2024
By Mari Selvan
πŸ‘οΈ 112 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
Java Program to Check Automorphic Number

Photo Credit to CodeToFun

πŸ™‹ Introduction

In the realm of Java programming, exploring unique number properties is both fascinating and educational. An automorphic number is one such interesting concept.

An automorphic number (or circular number) is a number whose square ends with the number itself. In simpler terms, an n-digit number is called an automorphic number if the last n digits of its square are equal to the number itself.

In this tutorial, we'll delve into a Java program that checks whether a given number is an automorphic number or not.

The program will take a number as input, square it, and then compare the last digits to determine if it meets the criteria of an automorphic number.

πŸ“„ Example

Let's explore the Java code that accomplishes this task.

AutomorphicNumber.java
Copied
Copy To Clipboard
public class AutomorphicNumber {

// Function to check if a number is automorphic
static boolean isAutomorphic(int num) {
  // Calculate the square of the number
  long square = (long) num * num;

  // Count the number of digits in the original number
  int originalDigits = 0;
  int temp = num;
  while (temp != 0) {
    temp /= 10;
    originalDigits++;
  }

  // Extract the last digits from the square
  long lastDigits = square % (long) Math.pow(10, originalDigits);

  // Check if the last digits match the original number
  return lastDigits == num;
}

// Driver program
public static void main(String[] args) {
  // Replace this value with your desired number
  int number = 76;

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

πŸ’» Testing the Program

To test the program with different numbers, simply replace the value of number in the main method.

Output
76 is an automorphic number.

Compile and run the program to check if the given number is an automorphic number.

🧠 How the Program Works

  1. The program defines a class AutomorphicNumber containing a static method isAutomorphic that takes a number as input and returns whether it is an automorphic number or not.
  2. Inside the method, it calculates the square of the input number.
  3. It counts the number of digits in the original number to extract the corresponding number of last digits from the square.
  4. It compares the extracted last digits with the original number to determine if it's an automorphic number.

πŸ“ Between the Given Range

Let's explore the Java code that checks for Automorphic Numbers in the specified range.

AutomorphicChecker.java
Copied
Copy To Clipboard
public class AutomorphicChecker {

  // Function to check if a number is Automorphic
  static boolean isAutomorphic(int num) {
    int square = num * num;
    String numStr = String.valueOf(num);
    String squareStr = String.valueOf(square);

    return squareStr.endsWith(numStr);
  }

  // Driver program
  public static void main(String[] args) {
    System.out.println("Automorphic Numbers in the range 1 to 50:");

    for (int i = 1; i <= 50; i++) {
      if (isAutomorphic(i)) {
        System.out.print(i + " ");
      }
    }
  }
}

πŸ’» Testing the Program

Output
Automorphic Numbers in the range 1 to 50:
1 5 6 25

The provided program is set to check for Automorphic Numbers within the range of 1 to 50. Run the program to see the Automorphic Numbers in this specific range.

🧠 How the Program Works

  1. The program defines a class AutomorphicChecker containing a static method isAutomorphic that checks if a given number is Automorphic.
  2. Inside the method, it calculates the square of the number and converts both the number and its square to strings.
  3. It checks if the square's string representation ends with the number's string representation.
  4. The main function tests and prints Automorphic Numbers in the range of 1 to 50.

🧐 Understanding the Concept of Automorphic Numbers

Before delving into the code, let's take a moment to understand automorphic numbers.

An n-digit number is called an automorphic number if the last n digits of its square are equal to the number itself.

For example, 25 is an automorphic number because its square is 625, and the last two digits (25) match the original number.

🎒 Optimizing the Program

While the provided program effectively checks for automorphic numbers, you can explore and implement further optimizations or variations based on your specific needs or preferences.

Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!

πŸ‘¨β€πŸ’» Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
10 months ago

If you have any doubts regarding this article (Java Program to Check Automorphic Number), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy