Menu driven program in java using switch case

MENU DRIVEN PROGRAM IN JAVA


Menu-driven program in java using switch case

Write a java program to print series 0, 3, 8, 15, 24, and s=1/2+3/4+5/6+..+19/20 with help of Menu using a switch case statement.

Solution:
import java.io.*;
public class MenuSeries
{
    public static void main(String args[])throws IOException
    {
        InputStreamReader in=new InputStreamReader(System.in);
        BufferedReader br=new BufferedReader(in);
        char choice;
        int n,i;
        double s=0;
        System.out.print("Enter n number:");
        n=Integer.parseInt(br.readLine());
        System.out.println("A or a for series 0,3,8,15,24……");
        System.out.println("B or b fro series s=1/2+3/4+5/6+..+19/20");
        System.out.println("Enter your choice:");
        choice=(char)br.read();
        switch(choice)
        {
            case 'A':
            case 'a':
           
                for(i=1;i<=n;i++)
                {
                    System.out.print((i*i)-1+",");
                }
            break;
            case 'B':
            case 'b':
           
                for(i=2;i<=20;i+=2)
                {
                    s=s+(double)(i-1)/i;
                }
                System.out.println("Sum="+s);
                break;
            default:
                System.out.print("Wrong choice.....");
        }
    }
}

Java Menu driven program to find Composite number and Smallest digit search

Java Menu driven program to find Composite number and Smallest digit search

import java.io.*;
public class Menu
{
    public static void main(String args[])throws IOException
    {
        InputStreamReader in=new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        int n, count, ch, small, a, i;
        System.out.println("Enter 1 for Composite number");
        System.out.println("Enter 2 for Smallest digit search");
        System.out.print("Enter your choice :");
        ch=Integer.parseInt(br.readLine());
        switch(ch)
        {
            case 1:
                count=0;
                System.out.print("Enter a number :");
                n= Integer.parseInt(br.readLine());
                for(i=2; i<=n/2; i++)
                {
                    if(n % i == 0)
                     count++;
                }
                if(count>0)
                 System.out.println("Entered number is Composite number");
                else
                 System.out.println("Entered number is not Composite number");
                break;
            case 2:
                System.out.print("Enter a number :");
                n= Integer.parseInt(br.readLine());
                small=99;
                while(n!=0)
                {
                    a = n % 10;
                    if(a < small)
                    {
                        small = a;
                    }
                    n = n / 10;
                }
                System.out.println("Smallest number is " + small);
                break;
            default:
                  System.out.println("Wrong choice");
        }
    }
               
}
menu driven calculator

MENU DRIVEN PROGRAM IN JAVA SIMPLE CALCULATOR

import java.io.*;
public class Menu
{
    private int a;  //Instance Variable
    private int b;
    private int c;
    private static InputStreamReader in=new InputStreamReader(System.in);
    private static BufferedReader br=new BufferedReader(in);
    void menu()
    {
        //System.out.println("\f");
        System.out.println("1. Addition");
        System.out.println("2. Subtraction");
        System.out.println("3. Multiplication");
        System.out.println("4. Division");
        System.out.println("5. Exit");
        System.out.println("Press your choice.........>");
    }

    void sum (int a, int b)
    {
        this.a=a;
        this.b=b;
        this.c=this.a+this.b;
        System.out.println("Sum="+this.c);
        // this.a=Integer.parseInt(br.readLine());
    }

    void sub (int x, int y)
    {
        a=x;
        b=y;
        c=a-b;
        System.out.println("Subtraction="+c);
    }

    void mult (int x, int y)
    {
        a=x;
        b=y;
        c=a*b;
        System.out.println("Multplication="+c);
    }

    void div (int x, int y)
    {
        a=x;
        b=y;
        c=a/b;
        System.out.println("Division="+c);
    }

    public static void main(String args[])throws IOException
    {
        Menu ob=new Menu();

        int ch,a,b;
        ch=0;
        do
        {
            ob.menu();
            ch=Integer.parseInt(br.readLine());
            switch(ch)
            {
                case 1:
                System.out.println("Enter two number");
                a= Integer.parseInt(br.readLine());
                b= Integer.parseInt(br.readLine());
                ob.sum(a, b);
                break;
                case 2:
                System.out.println("Enter two number");
                a=Integer.parseInt(br.readLine());
                b=Integer.parseInt(br.readLine());
                ob.sub(a, b);
                break;
                case 3:
                System.out.println("Enter two number");
                a=Integer.parseInt(br.readLine());
                b=Integer.parseInt(br.readLine());
                ob.mult(a, b);
                break;
                case 4:
                System.out.println("Enter two number");
                a=Integer.parseInt(br.readLine());
                b=Integer.parseInt(br.readLine());
                ob.div(a, b);
                break;
                case 5:
                System.exit(0);
                default:
                System.out.println("Wrong choice:");
            }
        }while(ch!=5);
    }       
}

Sample Output
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Press your choice.........>
1
Enter two number
2
5
Sum=7
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Press your choice.........>


5


Write a menu driven program to find the sum of the following series depending on the user choosing 1 or 2

1. S=1/4+1/8+1/12………upto n terms
2. S=1/1! - 2/2! + 3/3!…….upto n terms
Where ! Stands for factorial of the number and the factorial value of a number is the product of all integers from 1 to that number, e.g. 5! = 1 × 2 × 3 × 4 × 5.  (use switch-case).       


import java.util.*;
public class SeriesClass
{
    double s;
    int f, i, j , t, n;
    SeriesClass() // Constructor
    {
        s=0.0;
    }

    double series1(int no)
    {

        t=4;
        n=no;
        for(i=0; i<n; i++)
        {
            s = s + (double)1/t; // 1/4, 1/8, 1/12......
            t=t+4;
        }
        return s;
    }

    double series2(int no)
    {
        n=no;
        for(i=1; i<=n; i++)
        {
            f=1;
            //for factorial
            for(j=1;j<=i; j++)
            {
                f=f*j;
            } // end of factorial
            if(i%2==0)
            {
                s = s - (double)i/f;
            }
            else
            {
                s = s + (double)i/f;
            }
        }
        return s;
    }
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        SeriesClass ob=new SeriesClass();
        int ch, num;
        double sum;
        System.out.println("1 for S=1/4+1/8+1/12………upto n terms");
        System.out.println("2 for S=1/1! - 2/2! + 3/3!…….upto n terms");
        System.out.println("Enter your choice---->");
        ch=sc.nextInt();
        switch(ch)
        {
            case 1:
            System.out.println("Enter n terms:");
            num=sc.nextInt();
            sum=ob.series1(num);
            System.out.println("Sum="+sum);
            break;
            case 2:
            System.out.println("Enter n terms:");
            num=sc.nextInt();
            sum=ob.series2(num);
            System.out.println("Sum="+sum);
            break;
            default:
            System.out.println("Wrong Choice...");
        }
    }
}

Variable/Method Description or Documentation
Sr. No
Variable / Method
Type
Description
1
s
double
d is assign for Summation
2
I, j
int
i and j are use for Loop
3
t
int
For increment 4
4
n
int
N is use to assign the number
5
SeriesClass()

It is a constructor
6
series1(int no)
double
For the first series
7
Series2(int no)
double
For the second series
8
main()
void
This is a main method from where java program will run
9
sc
object
Object of Scanner class
10
ob
Object
Object of Class SeriesClass
11
ch
int
It is used for choice
12
num
int
For number
13
sum
double
For summation










SHARE THIS
Previous Post
Next Post