| by Arround The Web | No comments

How to Check Automorphic Numbers in Java

While programming in Java, there can be a requirement to filter out the values based on a particular check. For instance, utilizing the values meeting a certain requirement. In such a scenario, the “Automorphic” numbers come into effect that enables the developer to sort out the values based on a particular check.

This blog will demonstrate the approaches to checking for the “Automorphic” numbers in Java.

What is an “Automorphic” Number?

An “Automorphic” number corresponds to a number whose “square” has the same digits at the end as the number itself, i.e., 5, 6, 25, etc.

Demonstration

Let’s overview the following table to clear out the concept of the “Automorphic” numbers:

Number Square of Number Identical Digits(in end)
5 25 5
6 36 6
25 625 25

How to Check Automorphic Numbers in Java?

The “Automorphic” numbers in Java can be checked using the modulus operator “%” in combination with the comparison operator “==” and the “if/else” statement.

Example 1: Checking the Specified Integer For Automorphic Number in Java

This example checks a particular integer for an “Automorphic” number and returns the corresponding “boolean” outcome via the user-defined function:

public class Automorphic {

static boolean automorphicNum(int number) {

  int square = number * number;

while (number > 0) {

if (number % 10 == square % 10) {

return true;

}

else {

return false;

}}

return true;

}

public static void main( String args[] ) {

System.out.println("Is the number automorphic? \n"+automorphicNum(5));

}}

According to the above code, apply the following steps:

  • Firstly, define a “boolean” type function named “automorphicNum()” having the stated parameter that needs to be checked for an “Automorphic” number.
  • In the function definition, compute the “square” of the passed integer via the arithmetic operator “*”.
  • After that, in the “if” statement, compare the remainders of both the passed number and its square via the combined modulus operator “%” and the comparison operator “==” provided that the number is greater than “0” in the “while” loop.
  • Upon the comparison being satisfied, return the boolean outcome “true” indicating that the passed number is “Automorphic”. Otherwise, return “false”.
  • In the “main()” method, invoke the defined function and pass the stated integer that needs to be checked for the “Automorphic” number.

Output

As observed, the passed number, i.e., “5” is computed as “Automorphic” which is justified.

Before heading to the next example, make sure to import the following package to enable “user input”:

import java.util.Scanner;

Example 2: Checking the User Input Integers in the Defined Range For the Automorphic Numbers in Java

The following example applies a check upon the specified range of user input values for “Automorphic” numbers:

public class automorphic2 {

static boolean automorphicNum(int number) {

while (number > 0) {

if (number % 10 == Math.pow(number,2) % 10) {

return true;

}

else {

return false;

}}

return true; }

public static void main( String args[] ) {

  Scanner input = new Scanner(System.in);

System.out.println("Enter the start interval: ");

int x = input.nextInt();

System.out.println("Enter the end interval: ");

int y = input.nextInt();

System.out.println("Automorphic numbers between "+x+" and "+y);

for(int i=x; i<=y; i++){

if(automorphicNum(i))

System.out.print(i+" ");

input.close();

}}}

In this code snippet:

  • Recall the discussed approaches for defining a function returning a “boolean” outcome.
  • In its definition, apply a check upon the passed user input number via the discussed operators.
  • Note: Here, the “Math.pow()” method is used instead to compute the “square” of the passed user input numbers.
  • Now, in the “main()” method, create a “Scanner” object with the help of the “new” keyword and the “Scanner()” constructor, respectively.
  • The “System.in” parameter reads the user input.
  • After that, input the minimum and maximum integer values indicating the start and end intervals via the associated “nextInt()” method.
  • Lastly, apply the “for” loop to iterate along the values within the extreme intervals and log each of the numbers being “Automorphic” by passing the numbers to the invoked function and close the scanner.

Output

In this outcome, it can be implied that the numbers found to be “Automorphic” within the interval are returned accordingly.

Conclusion

The “Automorphic” numbers in Java can be checked using the modulus operator “%” in combination with the comparison operator “==” and the “if/else” statement. Alternatively, the “Math.pow()” method can also be applied to compute the square of the number and apply a check. This article elaborated on checking for the “Automorphic” numbers in Java.

Share Button

Source: linuxhint.com

Leave a Reply