0% found this document useful (0 votes)
6 views

Switch Case Solved Programs

Uploaded by

sarthak14jan09
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Switch Case Solved Programs

Uploaded by

sarthak14jan09
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

MENU DRIVEN/SWITCH CASE/USER’S CHOICE PROGRAMS

Program 1:
//Write a java program to device a calculator which is capable of doing basic
calculations
import java.util.*;//importing java package
class calculator//class declaration
{
public static void main(String args[])//method call
{

Scanner sc=new Scanner(System.in);

//initialising the variables


double n1,n2;
char ch;

System.out.println("Enter first number=");//print statement


n1=sc.nextDouble();//taking the input
System.out.println("Enter second number=");//print statement
n2=sc.nextDouble();//taking the input

//print statement

System.out.println(" Enter '+' for Addition,Enter '-' for Subtraction,Enter '*' for
Multiplication, Enter'/'for Division,Enter '%'for remainder");
System.out.println("Enter your choice");//print statement
ch=sc.next().charAt(0);//taking the input
double result;

result =0.0;
System.out.println("First number="+n1);//print statement
System.out.println("Second number="+n2);//print statement
switch (ch)//starting switch block
{
case '+'://first case for addition
result=n1+n2;

System.out.println("Addition of no.s="+result);//print statement


break;//applying break

case '-'://second case for subtraction


1
result=n1-n2;

System.out.println("Substraction of no.s="+result);//print statement


break;//applying break
case '*'://third case for multiplication
result=n1*n2;

System.out.println("Multiplication of no.s="+result);//print statement


break;//applying break

case '/'://fourth case for division


if (n2 ==0)//checking the conditon

System.out.println("Division error");//print statement


else
result=n1/n2;

System.out.println("Division of no.s="+result);//print statement


break;//applying break

case '%'://fifth case for remainder


if(n2==0)//checking condition

System.out.println("Division error");//print statement


else

result=n1%n2;

System.out.println("Result of modulus operator="+result);//print statement

break;//applying default

default://giving default

System.out.println("Invalid choice");//print statement


}
}
}

Program 2:
/*Program on The volume of solids, viz. cuboid, cylinder and cone can be
calculated by the formula:

2
Volume of a cuboid (v = l*b*h)
Volume of a cylinder (v = π*r^2*h)
Volume of a cone (v = (1/3)*π*r^2*h)
Using a switch case statement, write a program to find the volume of different
solids by taking suitable variables and data types.
*/
import java.util.*;

public class Volume


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("1. Volume of cuboid");
System.out.println("2. Volume of cylinder");
System.out.println("3. Volume of cone");
System.out.print("Enter your choice: ");
int choice = in.nextInt();

switch(choice) {
case 1:
System.out.print("Enter length of cuboid: ");
double l = in.nextDouble();
System.out.print("Enter breadth of cuboid: ");
double b = in.nextDouble();
System.out.print("Enter height of cuboid: ");
double h = in.nextDouble();
double vol = l * b * h;
System.out.println("Volume of cuboid = " + vol);
break;
case 2:
System.out.print("Enter radius of cylinder: ");
double rCylinder = in.nextDouble();
System.out.print("Enter height of cylinder: ");
double hCylinder = in.nextDouble();
double vCylinder = (22 / 7.0) * Math.pow(rCylinder, 2) * hCylinder;
System.out.println("Volume of cylinder = " + vCylinder);
break;

case 3:
System.out.print("Enter radius of cone: ");
double rCone = in.nextDouble();
System.out.print("Enter height of cone: ");
double hCone = in.nextDouble();
3
double vCone = (1 / 3.0) * (22 / 7.0) * Math.pow(rCone, 2) * hCone;
System.out.println("Volume of cone = " + vCone);
break;

default:
System.out.println("Wrong choice! Please select from 1 or 2 or 3.");
}
}
}
Program 3:

/*a program that displays the result of the following


evaluations based on the user's entry of choice from the keyboard
Natural logarithm of a number
Absolute value of a number
Square root of a number
Random number between 0 and 1
*/
import java.util.*;//importing java package
//class declaration
public class swi_m
{
//method declaration
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
//printing statement
System.out.println("****menu****");
System.out.println("1.Natural Logarithm");
System.out.println("2.Absolute value");
System.out.println("3.Square Root");
System.out.println("Random number between 0 and 1");
System.out.println("Please enter your choice");

int ch=sc.nextInt();

switch(ch)
{
case 1 :System.out.println("Enter a number whose logarithm is to be found");

int n=sc.nextInt();
double x=Math.log(n);
System.out.println("The logarithm of the number is:"+x);
4
break;
case 2:System.out.println("Enter a number whose Absolute value is to be found");;
int n1=sc.nextInt();
int m=Math.abs(n1);
System.out.println("The absolute value of the result is:"+m);
break;
case 3 :System.out.println("Enter a number whose square root is to be found");
int n2=sc.nextInt();
double m2=Math.sqrt(n2);
System.out.println("The square root of the result is:"+m2);
break;
case 4:double t=Math.random();
System.out.println(t);
break;
default :System.out.println("Invalid choice");
}
}
}

Program 4:
/*Using a switch case statement, write a menu driven program to convert a given
temperature from Fahrenheit to Celsius and
vice-versa. For an incorrect choice, an appropriate message should be
displayed.
*/
import java.util.*;
class Temperature
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double c,f, temperature; int conversion;
System.out.println("enter temparature");
temperature=sc.nextDouble();
System.out.println("enter 1 for Celcius, 2 for Fahrenhite");
System.out.println("Enter choice");
conversion=sc.nextInt();
switch(conversion)
{
case 1:

c=5*(temperature-32)/9;
5
System.out.println("Temperature in Celsius = "+c);
break;
case 2:
f=1.8*temperature+32;
System.out.println("Temperature in Fahrenheit = "+f);
break;
default:
System.out.println("wrong choice");
}
}
}

Program 5:

/*The Simple Interest (SI) and Compound Interest (CI) of a sum (P) for a given
time (T)
* and rate (R) can be calculated as:

(a) SI = (p * r * t) / 100 (b) CI = P * ((1 + (R / 100))T - 1)

Write a program to input sum, rate, time and type of Interest


('S' for Simple Interest and 'C' for Compound Interest).
Calculate and display the sum and the interest earned.
*/
import java.util.*;

public class Interest_SI_Compound


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the sum: ");
double p = in.nextDouble();
System.out.print("Enter rate: ");
double r = in.nextDouble();
System.out.print("Enter time: ");
int t = in.nextInt();
System.out.println("Enter type of Interest");
System.out.print("('S'- Simple Interest 'C'- Compound Interest): ");
char ch = in.next().charAt(0);
double interest = 0.0;

switch (ch) {
case 'S':
6
interest =( p * r * t) / 100;
double amt = p + interest;
System.out.println("Sum = " + p);
System.out.println("Interest = " + interest);
System.out.println("Amount = " + amt);
break;

case 'C':
interest = p * (Math.pow((1 + (r / 100)), t) - 1);
double amt1 = p + interest;
System.out.println("Sum = " + p);
System.out.println("Interest = " + interest);
System.out.println("Amount = " + amt1);
break;

default:

System.out.println("Incorrect Interest type");

}
}
}

Program 6:
/*The equivalent resistance of series and parallel connections of two resistances
are given by
* the formula:

(a) R1 = r1 + r2 (Series)
(b) R2 = (r1 * r2) / (r1 + r2) (Parallel)

Using a switch case statement, write a program to enter the value of r1 and r2.
Calculate and display the equivalent resistances accordingly.
*/
import java.util.Scanner;

public class Resistance


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("1. Series");
7
System.out.println("2. Parallel");

System.out.print("Enter your choice: ");


int ch = in.nextInt();
System.out.println("Enter r1: ");
double r1 = in.nextDouble();
System.out.println("Enter r2: ");
double r2 = in.nextDouble();
double eqr = 0.0;
switch (ch) {
case 1:
eqr = r1 + r2;
System.out.println("Equivalent resistance = " + eqr);
break;
case 2:
eqr = (r1 * r2) / (r1 + r2);
System.out.println("Equivalent resistance = " + eqr);
break;
default:

System.out.println("Incorrect choice");

}
}

Program 7:

/*A Mega Shop has different floors which display varieties of dresses as mentioned
below:
Ground floor : Kids Wear
First floor : Ladies Wear
Second floor : Designer Sarees
Third Floor : Men's Wear
The user enters floor number and gets the information regarding different items of the
Mega shop.
After shopping, the customer pays the amount at the billing counter and the
shopkeeper prints the bill in the given format:

Name of the Shop: City Mart


Total Amount:
8
Visit Again!!

Write a program to perform the above task as per the user's choice.
*/
import java.util.*;

public class Shop_Mega


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("1. Ground floor");
System.out.println("2. First floor");
System.out.println("3. Second floor");
System.out.println("4. Third floor");

System.out.print("Select a floor: ");


int f = in.nextInt();double amt=0.0;
switch (f) {
case 1:
System.out.print("Enter bill amount: ");
amt = in.nextDouble();
System.out.println("Kids Wear");
System.out.println("Name of the Shop: City Mart");
System.out.println("Total Amount: " + amt);
System.out.println("Visit Again!!");

break;
case 2:
System.out.println("Ladies Wear");
System.out.print("Enter bill amount: ");
amt = in.nextDouble();

System.out.println("Name of the Shop: City Mart");


System.out.println("Total Amount: " + amt);
System.out.println("Visit Again!!");
break;
case 3:
System.out.println("Designer Sarees");
System.out.print("Enter bill amount: ");
amt = in.nextDouble();

System.out.println("Name of the Shop: City Mart");


System.out.println("Total Amount: " + amt);
9
System.out.println("Visit Again!!");
break;
case 4:
System.out.println("Men's Wear");
System.out.print("Enter bill amount: ");
amt = in.nextDouble();

System.out.println("Name of the Shop: City Mart");


System.out.println("Total Amount: " + amt);
System.out.println("Visit Again!!");
break;
default:

System.out.println("Incorrect Floor");
break;

}
}

Program 8:

/* An electronic shop has announced the following discounts on purchase of


certain items
* PURCHASE AMOUNT Discount on Discount on
* in(Rs.) laptops Desktop PC
*
* 0 to 25,000 0.0% 5.0%
* 25,001 to 57,000 5.0% 7.5%
* 57,001 to 1,00,000 7.5% 10.0%
* More than 1,00,000 10.0% 15.0%
*
* Write a program to input name, amount of purchase and type of purchase(‘L’
for laptop & ‘D’ for Desktop) by the customer.
* Compute and print the net amount to be paid by a customer along with the
name.
*/
import java.util.*;//import java package
class computer_shop//declaring class
{//class starts
10
public static void main (String args[])//declaring main function
{//main function starts
char ch;int amt;String name;double dis;dis=0.0;double dist;dist=0.0;
Scanner sc=new Scanner(System.in);
System.out.println("Please enter your name");
name = sc.nextLine();
System.out.println("Please enter the amount");
amt = sc.nextInt();
System.out.println("Please enter your choice - L for Laptop & D for Desktop PC");
ch = sc.next().charAt(0);
switch(ch)
{
case'L'://case to compute the amount for a laptop
if(amt<=25000)
dis = 25000;
if(amt>25000&&amt<=57000)
dis =(int)( amt - (0.05*amt));//discount=amt*(double)5/100;
if(amt>57000&&amt<=100000)
dis = (int)(amt - (0.075*amt));
if(amt>100000)
dis = (int)(amt - (0.1*amt));
System.out.println("Mr."+name+"'s amount after deducting the discount for laptop is
Rs."+dis);
break;
case'D'://case to compute the amount of a desktop PC
if(amt<=25000)
dist = (int)(amt-(0.05*amt));
if(amt>25000&&amt<=57000)
dist = (int)(amt - (0.075*amt));
if(amt>57000&&amt<=100000)
dist = (int)(amt - (0.1*amt));
if(amt>100000)
dist = (int)(amt - (0.15*amt));
System.out.println("Mr."+name+"'s amount after deducting the discount for desktop PC
is Rs."+dist);
break;
default:
System.out.println("Wrong choice");
}//switch function ends
}//main function ends
}// class ends

11
Program 9:

/*Program that offers a menu to choose from various categories of cinema hall
tickets
*and asks for the number of tickets he/she buy after selecting a category and
then
*tell the total amount payable.The price-list is:-
*1.LOWER STALL 50/-
*2.UPPER STALL 70/-
*3.BALCONY 90/-
*4.BOX 120/-
*/
import java.util.*;//importing java package

class cinema//class declaration


{
public void main(String args[])//class declaration
{
Scanner sc= new Scanner(System.in);
//declaring and initialzing variables
int number=0;int ticket=0;int amount=0;

//print statement

System.out.println("1. Lower Stall");


System.out.println("2. Upper Stall");
System.out.println("3. Balcony");
System.out.println("4. Box");
System.out.println("Enter choice between 1 and 4");

number=sc.nextInt();//accepting choice from keyboard

switch(number)//starting switch block


{
//first case

case 1:System.out.print("How many lower stall-ticket?.");

ticket=sc.nextInt();
amount=ticket*50;//calculating amount
break;//applying break
12
//second case

case 2:System.out.print("How many upper stall=ticket?.");

ticket=sc.nextInt();
amount=ticket*70;//calculating amount
break;//applying break

//third case

case 3:System.out.print("How many Balcony-ticket?.");

ticket=sc.nextInt();
amount=ticket*90;//calculating amount
break;//applying break
//fourth case
case 4:System.out.print("How many Box-ticket?.");

ticket=sc.nextInt();
amount=ticket*120;//calculating amount
break;//applying break

//giving default
default:System.out.print("Unknown option");

}
System.out.println("Amount payable is :"+amount);//printing the amount to be paid
}
}

Program 10:

/*The relative velocity of two trains travelling in opposite directions is calculated


by adding their velocities. In case, trains are travelling in the same direction, the
relative velocity is the difference between their velocities. Write a program to
input the velocities and length of the trains. Write a menu driven program to
calculate the relative velocities and the time taken to cross each other by
indicating 1 for same direction and 2 for opposite direction.
*/
import java.util.*;
class Train_Velocity {
public static void main(String args[]) {
13
Scanner in = new Scanner(System.in);
System.out.println("Enter the Lenght of 1st Train :");
int t1 = in.nextInt();
System.out.println("Enter the Velocity of 1st Train:");
int v1 = in.nextInt();
System.out.println("Enter the Length of 2nd Train:");
int t2 = in.nextInt();
System.out.println("Enter the Velocity of 2nd Train:");
int v2 =in.nextInt();
System.out.println("Choose an Option:");
System.out.println("1:The Trains are Travelling in Same Direction.");
System.out.println("2:The Trains are Travelling in Opposite Direction.");
int ch = in.nextInt();
switch(ch) {
case 1:
System.out.println("The Relative Velocity :"+Math.abs(v1-v2));
System.out.println("Time Taken :"+((t1+t2)/(v1-v2))+"sec");
break;
case 2:
System.out.println("The Relative Velocity :"+(v1+v2));
System.out.println("Time Taken :"+((t1+t2)/Math.abs(v1+v2))+"sec");
break;
default:
System.out.println("Wrong Choice!!");
}
}
}

14

You might also like