Automorphic Number

package com.thealgorithms.maths;

/**
 * A number is said to be an Automorphic, if it is present in the last digit(s)
 * of its square. Example- Let the number be 25, its square is 625. Since,
 * 25(The input number) is present in the last two digits of its square(625), it
 * is an Automorphic Number.
 */
import java.io.*;

public class AutomorphicNumber {

    //returns True if the number is a Automorphic number and False if it is not an Automorphic number
    public static boolean isAutomorphic(int n) {
        int m, c, r, p, k;
        c = 0;
        /**
         * m = Temporary variable to store a copy of the number entered by the
         * user. n = The number entered by the user c = Count the digits of the
         * number entered by user. p = To calculate the square of the number. k
         * = Support variable to count the digits of the number
         */
        double s;
        m = n;
        p = m * m; //Calculating square of the number
        do {
            k = n / 10;
            c = c + 1; //Counting the digits of the number entered by user.
            n = k;
        } while (n != 0);
        s = Math.pow(10, c);
        r = p % (int) s;
        if (m == r) //Checking if the original number entered is present at the end of the square
        {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Method to check if number is Automorphic Number or Not 1) Input - Enter a
     * Number: 25 Output - It is an Automorphic Number. 2) Input - Enter a
     * Number: 7 Output - It is not an Automorphic Number.
     */
    public static void main(String args[]) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter a Number: ");
        int n = Integer.parseInt(br.readLine());
        if (isAutomorphic(n)) {
            System.out.println("It is an Automorphic Number.");
        } else {
            System.out.println("It is not an Automorphic Number.");
        }
    }
}
Algerlogo

Β© Alger 2022

About us

We are a group of programmers helping each other build new things, whether it be writing complex encryption programs, or simple ciphers. Our goal is to work together to document and model beautiful, helpful and interesting algorithms using code. We are an open-source community - anyone can contribute. We check each other's work, communicate and collaborate to solve problems. We strive to be welcoming, respectful, yet make sure that our code follows the latest programming guidelines.