Switch Case Solved Programs
Switch Case Solved 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
{
//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;
result=n1%n2;
break;//applying default
default://giving default
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.*;
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:
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:
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:
}
}
}
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;
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:
Write a program to perform the above task as per the user's choice.
*/
import java.util.*;
break;
case 2:
System.out.println("Ladies Wear");
System.out.print("Enter bill amount: ");
amt = in.nextDouble();
System.out.println("Incorrect Floor");
break;
}
}
Program 8:
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
//print statement
ticket=sc.nextInt();
amount=ticket*50;//calculating amount
break;//applying break
12
//second case
ticket=sc.nextInt();
amount=ticket*70;//calculating amount
break;//applying break
//third case
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:
14