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);
}
}
4 replies on “Automorphic Numbers using Recursive Method”
WAP in Java to enter a natural number ,where N>100 and N<1000,the natural number must not contain zeros. Print all the combinations of the digits of the number including the number itself. Each new combination should appear on a new line.
Sample Input:
N=465
Sample Output:
456
465
546
564
645
654
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
Here is the link to your program.
Sir,2 questions are given which states that:
Convert the following recursive codes to iterative codes
A) 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);
}
B) public static long getBinary(int n){
if(n == 0)
return 0L;
else{
int rem = n % 2;
return rem + getBinary(n / 2) * 10L;
}