×

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

Related Topics

Char and String differences in Java

Characters in Java Character (char) belongs to the characters group, which represents symbols in a character set, such as alphabets and numerals. A Java char has 16 bits in length and has a range...

5 minutes read.

Compare Two Times in Java

Definition The compareTo() function of the LocalTime class is used to compare times. The class's compareTo() method compares two LocalTime objects. Here we have two objects that have to be compared: the...

4 minutes read.

Java Class Name

How to write a class name? The following considerations should be made while writing class names. The name of the current class shouldn't be based on a preset or existing class. Java keywords...

3 minutes read.

flour pack problem in Java

Create a method called canPack and give it three int parameters: bigCount, smallCount, and objective. The bigCount option indicates the number of large flour bags (5 kilos each). The smallCount argument indicates...

3 minutes read.

Java String Interview Questions

Java String Interview Questions String is the most common and important discussed topics in a Java interview. In Java interview interviewer generally asked three to four questions based on String concept....

16 minutes read.

How to handle NullPointerException in Java

Introduction: Throwable class is the super or root class of the java exception hierarchy which contains two sub-classes. The classes’ are- ExceptionError Exception: If you are doing any wrong things in a...

3 minutes read.

This Operator Using in Java

this keyword in Java has a wide range of applications. this reference variable in Programming language refers to the object of interest. This keyword is used in Java. this keyword is...

5 minutes read.

Differences between Byte Code and Machine Code

This tutorial will discuss the differences between Byte Code and Machine Code. Before that, let's try to know what byte and machine code is. Introduction Every computer has a set of instructions...

4 minutes read.

How to Create a Generic List in Java?

Generics are types that have parameters. The goal is to make it possible for methods, classes, and interfaces to take type (Integer, String, etc., and user-defined types) as a parameter....

4 minutes read.

Copy data/content from one file to another in java

In this article, you will be acknowledged about how to copy data or content from one file to another file. Also, you will be acknowledged about the classes and methods...

3 minutes read.

Java Date add Days

In order to operate with the time and the Date in Java, we used the abstract Calendar class. It has several helpful interfaces that enable us to convert dates between...

4 minutes read.

Java Queue

The Java.util package has the interface Queue, which does extend the Collection interface. It is used to protect the parts that are managed using the FIFO approach. Being an interface, the...

5 minutes read.

Ternary Operator in Java

In some cases, the if...else expression in Java can be replaced by a ternary operator. Visit the Java if...else statement first before learning about the ternary operator. Ternary operator in Java A...

3 minutes read.

Undo and Redo Operations in Java

Undo and redo operation are the most widely used operation while dealing with file. In this section, we will discuss how to implement undo and redo operation in Java. Undo Redo...

2 minutes read.

Java List Node

In Java, List Node is the same as the single linked list, which is the collection of nodes. So, we can say, the list nodes are grouped together to get...

8 minutes read.

Convert JSON File to String in Java

Before understanding the conversion of JSON file to string, one must know about JSON. What is JSON? JSON stands for JavaScript Object Notation. It is an open standard format lightweight, text-based, and...

3 minutes read.

How Many Ways to Create Objects in Java

Introduction Java comes under the category of object-oriented programming languages. Since it is object-oriented, everything in Java is considered an object. Java is a diverse programming language that is designed to...

6 minutes read.

Java extend multiple classes

In Java, what does extend mean? One of several Java inheritance keywords is extended, meaning we pass all or most of the Parent class's characteristics through to the Child class. The...

3 minutes read.

List of Constants in Java

In this tutorial, we are going to deal with constants available in Java.Every programming language has its constants. Similarly, Javaalso has got constants of its own. In this tutorial, we will...

6 minutes read.

Classes and Objects in Java Example Programs

Classes and Objects in Java Example Programs Java is an Object-Oriented programming language, i.e., everything in Java is associated with objects and objects are associated with classes. The classes and objects...

5 minutes read.