Friday, September 30, 2011

Finding whether a number is an Automorphic number (Java)

//This Java program accepts a number from the user and
//the tells the user whether the number is automorphic
//or not.
//An automorphic number is a number whose square "ends"
//in the same digits as number itself. eg 762 =5776
import java.util.*;
class automorphic
{
    public static void main(String Args[])
    {
        System.out.println("Enter a number");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int d=digits(n);
        if(n==((n*n)%Math.pow(10,d)))
        {
            System.out.println("The number is automorphic");
        }
        else
        {
            System.out.println("The number is not automorphic");
        }
    }
   
    public static int digits(int a)
    {
        int k=0;
        while(a>0)
        {
            a=a/10;
            k++;
        }
        return(k);
    }
}
//Author : Mayank Rajoria

No comments:

Post a Comment