×

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

Thread States in Java

An Item's Life Cycle (Thread States) A thread can be in any of the states mentioned above at any given time in Java. They are as follows: New Active I'm blocked or waiting Temporary...

3 minutes read.

Use Of Adapter class in Java

An adapter class in Java enables listener interfaces to be implemented by default. The Delegation Event Model is where the idea of listener interfaces first appeared. It is one of...

3 minutes read.

Java Type Casting

Type casting is a technique or process used in Java to convert one data type into another, either manually or automatically. The compiler performs the automatic conversion, and the programmer...

3 minutes read.

Java Math copySign() Method

The copySign() method of Math class returns the first floating-point argument with the sign of the second argument. Syntax: public static float copySign(float magnitude, float sign)public static double copySign(double magnitude, double sign) Parameters: The...

1 minute read.

Java md5 Hash Example

A 128-bit hash value is generated by the Message Digest Algorithm 5, which is a cryptographic algorithm. A stationary hash value is generated by the hash function from data of...

3 minutes read.

Java Database Connectivity with MySQL

In this tutorial, we will learn how to connect Database with MySQL in Java. 5 Steps to Connect to the Database in Java Load the driver (or) Register the driver classEstablish a...

4 minutes read.

Java Buffer Reader

When a variable cannot change its value during run time is called a Static way of programming. In this programming, a variable is directly assigned to a value. Program: Import java.io.*; class  A {  ...

4 minutes read.

Multithreading in Java

Multithreading is a specialized form of multitasking. It is responsible for executing more than one task at a time of a single program, and each task is a separate thread. A program...

8 minutes read.

Convert list to array Java

One of the popular collection interfaces for storing an ordered collection is the List. The List interface may contain repeating groups and preserves the insertion order of entries. This article will...

4 minutes read.

Memory Leak in Java

Java offers memory management right out of the box. When we use the new keyword to create an object, the JVM initialises for that object immediately. The trash collector automatically...

3 minutes read.

JRE (Java Runtime Environment)

JRE is an installation package that provides an environment to run the Java program on any Operating System. It does not deal with the development process of any application. It is a part...

2 minutes read.

Java Math signum() Method

The signum() method of Java Math class returns the signum function of the value. Syntax: public static double signum(double d)public static float signum (float d) Parameters: The parameter ‘d’ represents the floating-point value whose...

2 minutes read.

Leap Year Program in Java

Leap Year Program in Java: A leap year is a calendar year that have an extra day i.e. 366 days instead of 365 days is called leap year. In other...

2 minutes read.

Lombok Java

What is Lombok java? A well-liked and widely-used Java framework that is used to reduce or eliminate boilerplate code is called Project Lombok. Both time and effort are saved. We may...

7 minutes read.

Types of Bitwise Operators in Java

In this tutorial, we will learn about the various types or kinds of bitwise operators in java. Before proceeding to bitwise operators, let us know what is meant by the...

6 minutes read.

Java calculate age

In this section, we will create a Java program that calculates age from the given date of birth or current date. In order to get the date of birth from the current...

6 minutes read.

Java Iterator

When iterating through, traversing, or retrieving the individual elements of a Collection or Flow object, a Java Cursor is leveraged as an iterator. In Java, there exist three types of...

4 minutes read.

Java vs Go

Java: Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say that...

4 minutes read.

Java Math.multiplyExact() method

In java, we are provided with multiplyExact method in the Math module. This Math module belongs to the java.lang package. In general, this method returns the product of the provided...

2 minutes read.

Java Applications

The growth in technology is increasing rapidly, so some languages are used for developing them. Java is one such famous programming language which is having numerous applications. The Java Programming...

4 minutes read.