Untitled

 avatar
unknown
plain_text
10 months ago
1.2 kB
18
Indexable
package LogicalProblems;
import java.util.Scanner;

public class AutomorphicNumber {

    /**
     *This class Takes  a positive integer N, determine whether it is an Automorphic Number.
     * A number is called an Automorphic Number if the square of the number ends with the number itself.
     *
     * Examples:
     * Input: N = 76
     * Output: true
     * Explanation: 76 * 76 = 5776 → ends in 76
     *
     * Input: N = 25
     * Output: true
     * Explanation: 25 * 25 = 625 → ends in 25
     */

    public static boolean checkAutomorphic(int n){

        long squareOfN = (long) n * n;
        long numCount = 0;
        long temp = n;
        long divisor = 1;
        boolean bool = false;

        while (temp > 0) {
            temp = temp / 10;
            numCount++;
        }

        for (int i = 1; i <= numCount; i++) {
            divisor = divisor * 10;
        }

        if (squareOfN % divisor == n) {
            bool = true;
        }
        return bool;
    }

    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);
        System.out.print ("ENTER A NUMBER -> ");
        int n = s.nextInt();

        System.out.println( n + " IS - > " + checkAutomorphic(n));
    }
}
Editor is loading...
Leave a Comment