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

Switch Statement

The document contains code snippets that demonstrate the use of switch statements in Java programs. The first snippet shows how to use a switch statement to print the name of a day based on the day number input by the user. The second snippet shows how to create a basic calculator program that uses a switch statement to perform different arithmetic operations like addition, subtraction, multiplication and division on two numbers based on the operation selected by the user.

Uploaded by

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

Switch Statement

The document contains code snippets that demonstrate the use of switch statements in Java programs. The first snippet shows how to use a switch statement to print the name of a day based on the day number input by the user. The second snippet shows how to create a basic calculator program that uses a switch statement to perform different arithmetic operations like addition, subtraction, multiplication and division on two numbers based on the operation selected by the user.

Uploaded by

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

Que: WAP to enter and display a string.

import java.util.Scanner;
class Stri
{
public static void main(String args[])
{
String str;
Scanner in=new Scanner(System.in);/* create a object */
System.out.println("Enter the string");
str=in.nextLine();
System.out.println("entered string="+str);
}
}

Switch statement to select one of many code blocks to be executed. Each value is
called a case.
Flow Diagram of Switch-case:

1
Que: WAP to enter the day number and display its name using switch case.

import java.util.Scanner;
class Day
{
public static void main(String args[])
{
int day;
Scanner in=new Scanner(System.in);
System.out.println("Enter the day number");
day=in.nextInt();

switch (day)
{
case 1: System.out.println("Monday");
break;
case 2: System.out.println("Tuesday");
break;
case 3: System.out.println("Wednesday");
break;
case 4: System.out.println("Thursday");
break;
case 5: System.out.println("Friday");
break;
case 6:System.out.println("Saturday ");
break;
case 7: System.out.println("Sunday ");
break;
default: System.out.println("Invalid day");
break;
}
}
}

Que 1: WAP to create the basic calculator for two numbers using switch case.

import java.util.Scanner;
class Calc
{
public static void main(String args[])
{
double a,b,sum,sub,multi,div;
int cal;
Scanner in = new Scanner(System.in);
System.out.print("Enter first number:");
2
a = in.nextDouble();
System.out.print("Enter second number:");
b = in.nextDouble();

System.out.print("Enter the option number\n ");


System.out.print("1 for Add\n 2 for Substract");
System.out.print("\n 3 for Multiplication\n 4 for Division");
cal= in.nextInt();

switch(cal)
{
case 1: sum=a+b;
System.out.println("Sum of a and b="+sum);
break;

case 2: sub=a-b;
System.out.println("Substraction of a and b="+sub);
break;

case 3: multi=a*b;
System.out.println("Multiplication of a and b="+multi);
break;

case 4: div=a/b;
System.out.println("Division of a and b="+div);
break;
default:
System.out.printf("You have entered wrong option");
}

}
}

You might also like