Friday 25 October 2013

Automorphic Numbers

import java.io.*;
class Automorphic_Numbers
{
 static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 boolean IsAutomorphic(int n)
 {
     int p=n*n,i=n,s=0,c=0;
  
     do
     {
         int d=p%10;
         s=s+d*(int)Math.pow(10,c++);
         i=i/10;
         p=p/10;
     }while(i!=0);
        if(n==s)
            return true;
        else
            return false;
 }

 void AutomorphicSeries()throws Exception
 {
     int pflag=0;
     System.out.print("\nEnter the Initial Number for Series : ");
     int ini=Integer.parseInt(br.readLine());
     System.out.print("Enter the Final Number for Series : ");
     int fin=Integer.parseInt(br.readLine());
     System.out.println("\nAutomorphic Number(s) from "+ini+" to "+fin+" is(are) : ");
     for(int i=ini;i<=fin;i++)
        if(IsAutomorphic(i))
          {System.out.println(i);pflag++;}
     if(pflag==0)
        System.out.println("No Automorphic Number Possible in the Series");
 }

 void AutomorphicCheck()throws Exception
 {
     System.out.print("\nEnter the Number to Check : ");
     int n=Integer.parseInt(br.readLine());
     if(IsAutomorphic(n))
       System.out.println(n+" is an Automorphic Number");
     else
       System.out.println(n+" is not an Automorphic Number");
 }

 public static void main(String args[])throws Exception
 {
     Automorphic_Numbers obj=new Automorphic_Numbers();
     int flag=0;
     do
     {
         System.out.println("\nAutomorphic Numbers\n-----------------");
         System.out.println("1 : Automorphic Numbers Check\n2 : Automorphic Numbers Series\n3 : Terminate Program");
         System.out.print("Enter Your Choice Code (1/2/3) : ");
         int ch=Integer.parseInt(br.readLine());
         switch(ch)
         {
             case 1:
             obj.AutomorphicCheck();
             break;
             case 2:
             obj.AutomorphicSeries();
             break;
             case 3:
             System.out.println("\nProgram Successfully Terminated");
             flag=1;
             break;
             default:
             System.out.println("Invalid Input ! Try Again");
             break;
         }
     }while(flag==0);
 }
}

No comments:

Post a Comment