0% found this document useful (0 votes)
68 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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 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
We take content rights seriously. If you suspect this is your content, claim it here.
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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy