×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Actual parameter and Formal parameter Perfect Numbers In Java - ICSE Examples
Menu Based Program in Java Using Switch case - Java
57632    Arnab De    05/05/2018

Write a menu driven Program in java to check whether a number is Prime No or Palindrome no.

All the menu driven program is coded using switch-case. We can write this type of programs in two way

1) Write the whole program in switch-case branches.

2) Create separate methods and call those methods from the switch-case branch.

Now we already explain the programs like prime no or palindrome no in our site although as this is different types of program, we write this again-

softetechnologies
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");
            }
       }
   }
}
softetechnologies

Output

  1. Prime no
  2. Palindrome no
  3. Quit

Enter Your Choice : 1

Enter A Number:31

31 is a prime Number

  1. Prime no
  2. Palindrome no
  3. Quit

Enter Your Choice : 1

Enter A Number:32

32 is not a prime Number

  1. Prime no
  2. Palindrome no
  3. Quit

Enter Your Choice : 2

Enter A Number:121

121 is a palindrom No.

  1. Prime no
  2. Palindrome no
  3. Quit

Enter Your Choice : 2

Enter A Number:122

122 is not a palindrom No.

  1. Prime no
  2. Palindrome no
  3. Quit

Enter Your Choice : 3

Actual parameter and Formal parameter Perfect Numbers In Java - ICSE Examples
softetechnologies
Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
Enter New Comment
Comment History
25/09/2021: I need help in my assignment. Can you please help?
By: Simran [simranpreet92@yahoo.in]
25/11/2021: thank you for the information you have written above. i am a new student in java programming and hopping to raise my ambition in the field to higher levels. please do post more so we as new in the field learn more thanks
By: nabita waluka [pauloneword@gmail.com]
Rabindranath Tagore
Everything comes to us if we create the capacity to receive it.
Rabindranath Tagore
6374
62.02
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     45779
25/06/2018     37975
01/01/2018     37714
28/06/2017     35689
02/08/2017     34347
01/08/2017     28667
06/07/2017     28419
15/05/2017     27969
14/07/2017     23778
11/09/2018     22614
softetechnologies