write a menu driven program to accept a number and check and display whether it's a prime number or not an automorpic number or not :[switch case statement ]
Answers
Answer:
import java.util.Scanner;
public class Check
{
public void PrimeorNot(int a)
{
if(a<=1)
{
System.out.println("It's not Prime");
}
else if(a==2)
{
System.out.println("It's a Prime");
}
else
{
boolean flag=true;
for(int i=2;i<a/2;i++)
{
if(a%i==0)
{
flag=false;
break;
}
}
if(flag)
{
System.out.println("It's a Prime ");
}
else
{
System.out.println("It's not a Prime ");
}
}
}
public void AutomorpicorNot(int b)
{
boolean flag=true;
int suare=b*b;
while(b>0)
{
if(b%10!=suare%10)
{
flag=false;
}
b/=10;
suare/=10;
}
if(flag)
{
System.out.println("It's an Automorpic");
}
else
{
System.out.println("It's not Automorpic");
}
}
public static void main(String[] args)
{
Check c=new Check();
Scanner scan=new Scanner (System.in);
boolean flag=true;
while(flag)
{
System.out.println("Select the option");
System.out.println("1. To check number is prime or not");
System.out.println("2. To check number is Automorpic or not");
System.out.println("3. Exit");
int i=scan.nextInt();
switch (i)
{
case 1:
System.out.println("Enter a number");
int a=scan.nextInt();
c.PrimeorNot(a);
break;
case 2:
System.out.println("Enter a number");
int b=scan.nextInt();
c.AutomorpicorNot(b);
break;
case 3:
flag=false;
break;
}
}
scan.close();
}
}
Program: Java:
import java.util.*;
public class Brainly
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,t,c=0,n,ch,r;
System.out.println("MENU");
System.out.println("1. to check for Prime Number");
System.out.println("2. to check for Automorphic Number");
System.out.println("Enter your choice:");
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter a number:");
n=sc.nextInt();
for(i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c==2)
System.out.println("Prime Number");
else
System.out.println("Not a Prime Number");
break;
case 2:
System.out.println("Enter a number:");
n=sc.nextInt();
t=n;
while(t!=0)
{
c++;
t=t/10;
}
if((n*n)%(int)Math.pow(10,c)==n)
System.out.println("Automorphic Number");
else
System.out.println("Not an Automorphic Number");
break;
default:
System.out.println("Invalid Choice!");
}
}
}