Java program for Prime no. and AutomorphicNumber

Java program for Prime no. and Automorphic Number

Automorphic Number

According to wikipedia "In mathematics an automorphic number (sometimes referred to as a circular number) is a number whose square "ends" in the same digits as the number itself. For example, 52 = 25, 62 = 36, 762 = 5776, and 8906252 = 793212890625, so 5, 6, 76 and 890625 are all automorphic numbers. The only automorphic Kaprekar number is 1, because the square of a Kaprekar number cannot start with zero. " Source: https://en.wikipedia.org/wiki/Automorphic_number.

Prime Number: The number divisible by 1 and by itself is called prime number.
Here using switch case (menu driven) we are going to write the following program.

import java.io.*;
public class MenuPrimeAutomorphicNumber
{
   public static void main(String args[])throws IOException
   {
       int i,ch,a,no,m,f=1,c=0,p;
       InputStreamReader in=new InputStreamReader(System.in);
       BufferedReader br=new BufferedReader(in);
       System.out.println("1. For Prime number");
       System.out.println("2. For Automorphic number");
       System.out.print("Enter your choice:");
       ch=Integer.parseInt(br.readLine());
       switch(ch)
       {
           case 1:
            System.out.print("Enter number to check prime:");
            no=Integer.parseInt(br.readLine());
            for(i=2;i<no;i++)
            {
                if(no%i==0)
                {
                    f=0;
                    break;
                }
            }
            if(f==1)
             System.out.print("Prime number");
            else
             System.out.print("Not Prime number");
            break;
           case 2:
            System.out.print("Enter number to check Automorphic:");
            no=Integer.parseInt(br.readLine());
            m=no*no;
            while(m!=0)
            {
                m/=10;
                c++;
            }
            p=(int)Math.pow(10,c-1);
            a=no%p;
            if(no==a)
             System.out.println("Automorphic number");
            else
             System.out.println("Not Automorphic number");
            break;
           default:
            System.out.println("Wrong choice");
        }
    }
             
}

Output

1. For Prime number
2. For Automorphic number
Enter your choice:1
Enter number to check prime:5

Prime number
1. For Prime number
2. For Automorphic number
Enter your choice:2
Enter number to check Automorphic: 25
Automorphic number

Also visit
Kaprekar number



SHARE THIS
Previous Post
Next Post