please explain menu driven program in java. no spam please i want defination
Answers
Answer:
All the menu driven program is coded using switch-case.
which asks user to chose the function to be performed, this is facilitated by the input given by the user. to explain in better way below is a program which use switch case.
import java.util.*;
public class PrimeAndPalindrome
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
int ch,k;
while(true)
{
System.out.println("01. Prime no");
System.out.println("02. Palindrome no");
System.out.println("03. Quit");
System.out.print("Enter Your Choice : ");
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.print("Enter A Number:");
k=sc.nextInt();
int d=2;
boolean b=true;
while(d<=k/2)
{
if(k%d==0)
{
b=false;
break;
}
d++;
}
if(b)
{
System.out.print(k + " is a prime Number");
}
else
{
System.out.print(k + " is not a prime Number");
}
break;
case 2:
System.out.print("Enter A Number:");
k=sc.nextInt();
int rev=0,temp=k;
while(temp>0)
{
rev = (rev*10) + (temp%10);
temp /= 10; // temp=temp/10
}
if(k==rev)
{
System.out.println(k + " is a palindrom No.");
}
else
{
System.out.println(k + " is not a palindrom No.");
}
break;
case 3:System.exit(0);
default: System.out.println("Wrong Entry");
}
}
}
}
Output
01. Prime no
02.Palindrome no
03.Quit
Enter Your Choice : 1
Enter A Number:31
31 is a prime Number
01. Prime no
02.Palindrome no
03.Quit
Enter Your Choice : 1
Enter A Number:32
32 is not a prime Number
01. Prime no
02.Palindrome no
03.Quit
Enter Your Choice : 2
Enter A Number:121
121 is a palindrom No.
01. Prime no
02.Palindrome no
03.Quit
Enter Your Choice : 2
Enter A Number:122
122 is not a palindrom No.
01. Prime no
02.Palindrome no
03.Quit
Enter Your Choice : 3
I hope this helped you so plz rate accordingly...