Using switchcase statement write a code that takes a number
Solution
//Program 8
import java.util.Scanner;
public class Conditionals {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(\"Enter the number:\");
int num = input.nextInt(); // Reading vakueus using Scanner object with nextInt()
switch(num){ // Switch cases for 1, 2 3, and others
case 1: System.out.println(\"Double Double burger\");
break;
case 2: System.out.println(\"Cheese Burger\");
break;
case 3: System.out.println(\"Hambugger\");
break;
default: System.out.println(\"Invalid Choice\");
break;
}
}
}
Output:
Enter the number:
3
Hambugger
Enter the number:
2
Cheese Burger
Enter the number:
5
Invalid Choice
//Program 9
import java.util.Scanner;
public class Foot {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(\"Enter the Foot Size:\");
int size = input.nextInt(); //nextInt() method fro integer
if(size>=0 && size<6){ //Condition for size of foot wear
System.out.println(\"Small\");
} else if(size>=6 && size<=8){
System.out.println(\"Medium\");
} else{
System.out.println(\"Large\");
}
}
}
Output:
Enter the Foot Size:
4
Small
Enter the Foot Size:
7
Medium
Enter the Foot Size:
9
Large
//Program 10
import java.util.Scanner;
public class SmallReal {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(\"Enter the two real number:\");
double num1 = input.nextDouble(); // Reading double real values
double num2 = input.nextDouble();
if(num1>num2){
System.out.println(\"Smallest is:\"+num2);
} else if(num1==num2){
System.out.println(\"Values are Same:\"+num1);
} else {
System.out.println(\"Smallest is:\"+num1);
}
}
}
Output
Enter the two real number:
5
9.8
Smallest is:5.0
Enter the two real number:
5.6
5.6
Values are Same:5.6
//Program 11
import java.util.Scanner;
public class Genders {
public static void main(String[] args) {
char mate1,mate2;
Scanner input = new Scanner(System.in);
System.out.println(\"Enter the Genders:\");
mate1 = input.next().charAt(0); // First character in String is Character i.e Male and Female with M and F
mate2 = input.next().charAt(0);
if(mate1==\'M\' && mate2==\'M\'){
System.out.println(\"They can be roommates in Smithhall\");
} else if(mate1==\'F\' && mate2==\'F\'){
System.out.println(\"They can be roommates in Simmons hall\");
} else {
System.out.println(\"They cannot be roommates\");
}
}
}
Output
Enter the Genders:
M
M
They can be roommates in Smithhall
Enter the Genders:
F
F
They can be roommates in Simmons hall
Enter the Genders:
M
F
They cannot be roommates


