Question
Write a menu driven program to accept a number and check and display whether it is a Prime Number or not OR an Automorphic Number or not. (Use switch-case statement).
(a) Prime number : A number is said to be a prime number if it is divisible only by 1 and itself and not by any other number. Example : 3,5,7,11,13 etc.
(b) Automorphic number : An automorphic number is the number which is contained in the last digit(s) of its square.
Example: 25 is an automorphic number as its square is 625 and 25 is present as the last two digits.
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class Program7
{
public static void main(String[]args)
{
int choice,n,i=0,count=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter 1 for Prime number");
System.out.println("Enter 2 for Automorphic number");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
switch(choice)
{
case 1:
System.out.print("Enter a no. ");
n = sc.nextInt();
for(i=1;i<=n;i++)
{
if(n%i==0)
{
count++;
}
}
if(count == 2)
{
System.out.println(n+" is a Prime no.");
}
else
{
System.out.println(n+" is not a Prime no.");
}
break;
case 2:
System.out.print("Enter a no. ");
n = sc.nextInt();
int temp,rem;
temp = n;
/*counting digits in number 'n'*/
while(temp>0)
{
temp/=10;
count++;
}
rem = (n*n) % (int)Math.pow(10,count);
if(rem==n)
{
System.out.println(n+" is an Automorphic no. ");
}
else
{
System.out.println(n+ " is Not an Automorphic no. ");
}
break;
default:
System.out.println("Invalid Choice");
}
}
}