Thursday, June 13, 2013

Automorphic Number in Java

An automorphic number is a number whose square ends in the same digits as the number itself.
For example, 25 is an automorphic number because 252 = 625, and 625 ends with 25, which is the number itself. Below is the program written in Java to check if a given integer is an automorphic number:

import java.io.*;
class Automorphic{
public static void main(String args[])throws IOException{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
int num, result;
System.out.print("Enter the integer:");
num=Integer.parseInt(br.readLine());
int countDigits=count(num);
int divisor=(int)Math.pow(10, countDigits);
result=(num*num)%divisor;
if(num==result)
System.out.println(num+" is an automorphic number.");
else
System.out.println(num+" is not an automorphic number.");
}
public static int count(int num){
int i, countDigits=0;
for(i=num;i>0;i/=10)
countDigits++;
return countDigits;
}
}

No comments:

Post a Comment