Automorphic Numbers using Recursive Method

Write a program to allow the user to enter a positive integer.

Now check if the entered number is an automorphic number or not, using a recursive method.

An automorphic number is one whose square ends with the original number itself.

Example:
INPUT: 25
OUTPUT: 25 is automorphic.

A recursive method is one that calls itself either directly or indirectly.

import java.io.*;
class Automorphic{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter the number: ");
        int num = Math.abs(Integer.parseInt(br.readLine()));
        if(isAutomorphic(num, 10))
            System.out.println(num + " is automorphic.");
        else
            System.out.println(num + " is not automorphic.");
    }
    public static boolean isAutomorphic(int num, int p){
        if(p > num * num)
            return false;
        else if((num * num) % p == num)
            return true;
        else
            return isAutomorphic(num, p * 10);

    }
}

2 thoughts on “Automorphic Numbers using Recursive Method

  1. Sir,a program is given which states that:
    WAP in Java to aceept n numbers in a single dimensional array. Display the numbers after eliminating duplicate numbers of the array.
    Sample Input:n=10
    69
    45
    45
    25
    34
    40
    34
    41
    29
    16
    Sample Output:
    The result after removing duplicate numbers:
    69 45 25 34 40 41 29 16

Leave a Reply