Sharpen Your Skills: Programming Languages Practice Q&A

Programming practice questions

Java if else, if else ladder and switch Questions

 
1

Question- Write a program for checking enter number is even or odd.



Answer-
  package csdtpatna;
  import java.util.Scanner;
  class EvenOdd
  {
  public static void main(String []args)
 {
//Creating object of Scanner class for taking input from User.
  Scanner sc=new Scanner(System.in);
  System.out.println("Enter a number");
 int n=sc.nextInt();
 if(n%2==0)
 {
 System.out.println("Number is Even");
 }
 else
 {
 System.out.println("Number is Odd");
 }

 }

 }
2

Question- Write a program to take two number from user and check who is max.



Answer-
package csdtpatna;
import java.util.Scanner;
class MaxMin
{
public static void main(String []args)
{
//Creating object of Scanner classfor taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter two number");
int n1=sc.nextInt();
int n2=sc.nextInt();
if(n1==n2)
{
System.out.println("Both Number is Equal:: n1="+n1+" And n2="+n2);
}
else if(n1>n2)
{
System.out.println("Number N1 is Max::"+n1);
}
else
{
System.out.println("Number N2 is Max::"+n2);
}

}

}
3

Question- Check whether an alphabet is vowel or consonant using if..else statement.



Answer-
 public class VowelConsonant 
 {
    public static void main(String[] args) {
        char ch = 'i';
        if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' )
            System.out.println(ch + " is vowel");
        else
            System.out.println(ch + " is consonant");
    }
  }
4

Question- Write a program for checking enter number is Positive or Negative.



Answer-

package csdtpatna;
import java.util.Scanner;
class NegativePositive
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
if(n>=0)
{
System.out.println("Number is Positive");
}
else
{
System.out.println("Number is Negative");
}

}

}
5

Question- Write a program to take input age of person and check he is eligible for marriage or not.



Answer-
package csdtpatna;
import java.util.Scanner;
class Marriage
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter age of Person");
int age=sc.nextInt();
if(age>=21)
{
System.out.println("Person is eligible for marriage");
}
else
{
System.out.println("Person is not eligible for marriage");
}

}

}
6

Question- Write a program to take input age of male or Female and check he/she is eligible for marriage or not.



Answer-
package csdtpatna;
import java.util.Scanner;
class Marriage2
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter age of Male or Female");
int age=sc.nextInt();
System.out.println("Enter Gender of Human like male or female");
String gen=sc.nextLine();
if(gen=="male")
{

if(age>=21)
{
System.out.println("Male is eligible for marriage becz age is:"+age);
}
else
{
System.out.println("Male is not  eligible for marriage becz age is:"+age);
}
}

else if(gen=="female")
{
if(age>=18)
{
System.out.println("female is eligible for marriage becz age is:"+age);
}
else
{
System.out.println("female is not  eligible for marriage becz age is:"+age);
}
}
else
{

System.out.println("Not eligible for marriage becz gen is NNN:");
}
}

}
7

Question- Write a program to take input age of person and job status like yes or no and check he is eligible for marriage or not.



Answer-
package csdtpatna;
import java.util.Scanner;
class Marriage3
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter age of Person");
int age=sc.nextInt();
System.out.println("Enter Job stauts like yes or no");
String job=sc.nextLine();
if(job=="yes")
{
System.out.println("Person is eligible for marriage a/c to job status");

if(age>=21)
{
System.out.println("Person is eligible for marriage becz age is:"+age);
}
else
{
System.out.println("Person is not  eligible for marriage becz age is:"+age);
}
}

else
{
System.out.println("Person is not  eligible for marriage becz no job");
}

}

}
8

Question- Write a java program to input a cost of a pen and calculate 70 pens cost with some discount. if total pen cost greater than 1000 then we calculate 20% discount otherwise 10%.



Answer-
package csdtpatna;
import java.util.Scanner;
class PenCost
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter Cost of a Pen");
int one_pen_cost=sc.nextInt();
int TotalCost=70*one_pen_cost;
int dis;
if(TotalCost>=1000)
{
dis=TotalCost*20/100;
System.out.println("Dear customer you Pay only Rs. "+(TotalCost-dis));
}
else
{
dis=TotalCost*10/100;
System.out.println("Dear customer you Pay only Rs. "+(TotalCost-dis));
}
System.out.println("One Pen Cost is:: Rs. "+one_pen_cost);
System.out.println("Total Pen Cost without discount is:: Rs. "+TotalCost);
System.out.println("Total discount Amount is:: Rs. "+dis);
}

}
9

Question- Write a java program to take Basic salary of employee from user and calculate gross salary with given condition, if Basic salary >= 10000 then we calculate 40% da and 30% ta of Basic salary otherwise 30% da and 20% ta of Basic salary.



Answer-
package csdtpatna;
import java.util.Scanner;
class EmployeeSalary
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter Basic Salary");
int bs=sc.nextInt();
int da,ta,gs;
if(bs>=10000)
{
da=bs*40/100;
ta=bs*30/100;

}
else
{
da=bs*30/100;
ta=bs*20/100;
}
System.out.println("Basic Salary is:: Rs. "+bs);
System.out.println("DA is:: Rs. "+da+" and TA is Rs "+ta);
System.out.println("Gross Salary is:: Rs. "+(bs+da+ta));
}

}
10

Question- Write a java program to Take values of length and breadth of a rectangle from user and check if it is square or not.



Answer-
import java.util.Scanner;
 class Rectangle
  {
  public static void main(String[] args)
  { //Creating object of Scanner Class
    Scanner s = new Scanner(System.in);
    System.out.println("Enter length");
    int x = s.nextInt();
    System.out.println("Enter breadth");
    int y = s.nextInt();
    if(x==y)
    {
      System.out.println("Square");
    }
    else
    {
      System.out.println("Rectangle");
    }
  }
}
11

Question- Write a java program to A shop will give discount of 10% if the cost of purchased quantity is more than 2000. Ask user for quantity, Suppose, one unit will cost 100. Judge and print total cost for user.



Answer-
import java.util.Scanner;
class Shopping{
  public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    System.out.println("Enter quantity");
    int x = s.nextInt();
    int T=x*100;
    if(T>2000)
    {
      System.out.println("You get a discount of "+(.1*T)+" and your total cost is "+(T-(.1*T)));
    }
    else
   {
      System.out.println("No discount, you pay only Rs "+T);
    }
  }
}
12

Question- Write a java program to A company decided to give bonus of 5% to employee if his/her year of service is more than 5 years. Ask user for their salary and year of service and print the net bonus amount.



Answer-
import java.util.Scanner;
class Bonus{
  public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    System.out.println("Enter Current Salary");
    int cs = s.nextInt();
    System.out.println("Enter Year of service");
    int y = s.nextInt();
    
    if(y>5)
    {
      System.out.println("You get a bonus of "+(cs*5/100)+" and your total Salary is "+(cs+cs*5/100)));
    }
    else
   {
      System.out.println("No Bonus, your Salary is "+cs);
    }
  }
}
13

Question- Write a java program to A school has following rules for grading system: a. Below 25 - F b. 25 to 45 - E c. 45 to 50 - D d. 50 to 60 - C e. 60 to 80 - B f. Above 80 - A ,Ask user to enter marks and print the corresponding grade.



Answer-
import java.util.Scanner;
 class Grading{
  public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    System.out.println("Enter your marks between 1 to 100");
    int x = s.nextInt();
    if(x<25)
    {
      System.out.println("F");
    }
    else if((x>=25)&&(x<45))
    {
      System.out.println("E");
    }
    else if((x>=45)&&(x<50))
   {
      System.out.println("D");
    }
    else if((x>=50)&&(x<60))
    {
      System.out.println("C");
    }
    else if((x>=60)&&(x<80))
    {
      System.out.println("B");
    }
    else if((x>=80)&&(x<=100))
    {
      System.out.println("A");
    }
    else
    {
      System.out.println("Not correct marks");
    }
  }
}
14

Question- Write a java program to Check enter number is perfect number or not perfect number.



Answer-
import java.io.*;
 import java.util.Scanner;
 import java.lang.Math;
 public class PerfectNumber 
   {
 public static void main(String[] args) 
      { 
  Scanner sc= new Scanner(System.in);
  System.out.println("Enter a number");     
  int n=sc.nextInt();
  int sqrt=(int)Math.sqrt(n);
  if(sqrt*sqrt==n)
 {
  System.out.println("Yes");
 }
  else
 {
    System.out.println("No");
 }
 }
 }

15

Question- Write a program to check Two input numbers are equels or not.



Answer-
import java.io.*;
 import java.util.Scanner;
 public class Test 
 {
 public static void main(String[] args) 
 { 
 Scanner sc= new Scanner(System.in);
 double a,b;
  System.out.println("Enter 1st number");
 a=sc.nextDouble();
 System.out.println("Enter 2nd number");
 b=sc.nextDouble();
 if(a==b)
 {
 System.out.println("They are the same");
  }
  else
  {
    System.out.println("They are different");
   }
  }
 }
16

Question- write a program to check Triangle is valid or not.



Answer-
import java.io.*;
 import java.util.Scanner;
 public class Test
  {
 public static void main(String[] args) 
 {
   Scanner sc= new Scanner(System.in);
 System.out.println("Enter traingle side1");
   int n1=sc.nextInt();
 System.out.println("Enter traingle side2");
   int n2=sc.nextInt();
 System.out.println("Enter traingle side3");
   int n3=sc.nextInt();
   int s=n1+n2+n3;
   if(s==180)
      System.out.println("Traingle is valid");
   else
      System.out.println("Traingle is not valid");
   }
 }
17

Question- Write a program to Take input of age of 3 people by user and determine oldest among them.



Answer-
import java.io.*;
 import java.util.Scanner;
 public class Test 
  {
  public static void main(String[] args) 
    { 
  Scanner sc = new Scanner (System.in);
  System.out.println("Enter age of 1st person");
       int a=sc.nextInt();
 System.out.println("Enter age of 2nd person");
       int b=sc.nextInt();
 System.out.println("Enter age of 3rd person");
       int c=sc.nextInt();
       if((a>b)&&(a>c))
         System.out.println(" 1st Person is Oldest bcz its age is"+a);
       else if(b>c)
         System.out.println(" 2nd Person is Oldest bcz its age is"+b);
       else
   System.out.println(" 3rd Person is Oldest bcz its age is"+c);
      
}
 }
18

Question- Enter age and check eligible for vote or not.



Answer-
import java.io.*;
import java.util.Scanner;
public class Test
  {
  public static void main(String[] args) 
    { 
  Scanner sc = new Scanner (System.in);
 System.out.println("Enter age");
       int a=sc.nextInt();
       if(a<18)
         System.out.println("You are not eligible to vote");
       else
         System.out.println("You are eligible to vote");
      
}
 }
19

Question- Write a program to print absolute vlaue of a number entered by user. E.g.- INPUT: 1 OUTPUT: 1 INPUT: -1 OUTPUT: 1



Answer-
class Test{
  public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    System.out.println("Enter number");
    int x = s.nextInt();
    if(x<0){
      System.out.println("Absolute value : "+(-1*x));
    }
    else{
      System.out.println("Absolute value : "+x);
    }
  }
 }
20

Question- Write a Java program to solve quadratic equations.



Answer-
import java.util.Scanner;
public class Exercise2 {

    
  public static void main(String[] Strings)
{
 Scanner input = new Scanner(System.in);

  System.out.print("Input a: ");
  double a = input.nextDouble();
  System.out.print("Input b: ");
  double b = input.nextDouble();
  System.out.print("Input c: ");
  double c = input.nextDouble();

  double result = b * b - 4.0 * a * c;

   if (result > 0.0) {
     double r1 = (-b + Math.pow(result, 0.5)) / (2.0 * a);
     double r2 = (-b - Math.pow(result, 0.5)) / (2.0 * a);
     System.out.println("The roots are " + r1 + " and " + r2);
            }
      else if (result == 0.0) {
        double r1 = -b / (2.0 * a);
        System.out.println("The root is " + r1);
            }
      else {
  System.out.println("The equation has no real roots.");
       }
    }
}
21

Question- Write a Java program that takes a year from user and print whether that year is a leap year or not. Input the year: 2016 Expected Output : 2016 is a leap year



Answer-
import java.util.Scanner;
public class Exercise9 {

    
  public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);

        System.out.print("Input the year: ");
        int year = in.nextInt();

        boolean x = (year % 4) == 0;
        boolean y = (year % 100) != 0;
        boolean z = ((year % 100 == 0) && (year % 400 == 0));

        if (x && (y || z))
        {
            System.out.println(year + " is a leap year");
        }
        else
        {
            System.out.println(year + " is not a leap year");
        }
    }
}
22

Question- Check whether an alphabet is vowel or consonant using switch statement.



Answer-
public class VowelConsonant {
    public static void main(String[] args) {
        char ch = 'z';
        switch (ch) {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
                System.out.println(ch + " is vowel");
                break;
            default:
                System.out.println(ch + " is consonant");
        }
    }
}
23

Question- Write a Java program to find the number of days in a month.



Answer-
import java.util.Scanner;
public class Exercise7 {

    
  public static void main(String[] strings) {

        Scanner input = new Scanner(System.in);

        int number_Of_DaysInMonth = 0; 
        String MonthOfName = "Unknown";

        System.out.print("Input a month number: ");
        int month = input.nextInt();

        System.out.print("Input a year: ");
        int year = input.nextInt();

        switch (month) {
            case 1:
                MonthOfName = "January";
                number_Of_DaysInMonth = 31;
                break;
            case 2:
                MonthOfName = "February";
                if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
                    number_Of_DaysInMonth = 29;
                } else {
                    number_Of_DaysInMonth = 28;
                }
                break;
            case 3:
                MonthOfName = "March";
                number_Of_DaysInMonth = 31;
                break;
            case 4:
                MonthOfName = "April";
                number_Of_DaysInMonth = 30;
                break;
            case 5:
                MonthOfName = "May";
                number_Of_DaysInMonth = 31;
                break;
            case 6:
                MonthOfName = "June";
                number_Of_DaysInMonth = 30;
                break;
            case 7:
                MonthOfName = "July";
                number_Of_DaysInMonth = 31;
                break;
            case 8:
                MonthOfName = "August";
                number_Of_DaysInMonth = 31;
                break;
            case 9:
                MonthOfName = "September";
                number_Of_DaysInMonth = 30;
                break;
            case 10:
                MonthOfName = "October";
                number_Of_DaysInMonth = 31;
                break;
            case 11:
                MonthOfName = "November";
                number_Of_DaysInMonth = 30;
                break;
            case 12:
                MonthOfName = "December";
                number_Of_DaysInMonth = 31;
        }
        System.out.print(MonthOfName + " " + year + " has " + number_Of_DaysInMonth + " days\n");
    }
}

Copy
Sample Output:

Input a month number: 2                                                                                       
Input a year: 2016                                                                                            
February 2016 has 29 days

24

Question- Write a Java program to take two Input number is equal or not.



Answer-
import java.io.*;
import java.util.Scanner;
public class TestClass {
public static void main(String[] args) { 
       Scanner sc = new Scanner(System.in);
         double a,b;
         a = sc.nextDouble();
         b = sc.nextDouble();
        if(a == b)
        {
          System.out.println("They are the same");
        }
       else
       {
         System.out.println("They are different");
       }
}
}

25

Question- Explain the difference between if, if-else, and if-else-if statements.



Answer-

DifferencesBetween if, if-else, and if-else-if Statements

1.    ifStatement:

o   The if statement is used to execute a block ofcode only if a specified condition is true.

o   If the condition is false, the code inside theif block will not be executed.

Syntax: java code

if (condition)

 {

   // code to execute if condition is true

}

Example: java code

int age = 18;

if (age >= 18) {

   System.out.println("You are eligible to vote.");

}

2.    if-elseStatement:

o   The if-else statement is used to execute oneblock of code if the condition is true, and another block if the condition isfalse.

o   It provides a default action when the ifcondition is not met.

Syntax: java code

if (condition)

{

   // code to execute if condition is true

}

else

{

   // code to execute if condition is false

}

Example: Java code

int age = 16;

if (age >= 18)

{

   System.out.println("You are eligible to vote.");

} else {

   System.out.println("You are not eligible to vote.");

}

3.    if-else-ifStatement:

o   The if-else-if statement is used to checkmultiple conditions sequentially.

o   It allows you to execute different blocks ofcode based on different conditions.

o   The first condition that evaluates to truewill have its corresponding block executed, and the rest will be skipped.

o   If none of the conditions are true, anoptional else block can be used as a fallback.

Syntax: java code

if (condition1) {

   // code to execute if condition1 is true

} else if (condition2) {

   // code to execute if condition2 is true

} else if (condition3) {

   // code to execute if condition3 is true

} else {

   // code to execute if none of the conditions are true

}

Example: java code

int score = 75;

if (score >= 90) {

   System.out.println("Grade: A");

} else if (score >= 80) {

   System.out.println("Grade: B");

} else if (score >= 70) {

   System.out.println("Grade: C");

} else {

   System.out.println("Grade: D");

}

Summary:

  • if checks a single condition and runs code if it's true.
  • if-else checks a single condition, runs code if it's true, otherwise runs a different block if it's false.
  • if-else-if checks multiple conditions in sequence and runs the code for the first true condition, with an optional else for when none are true.

 


26

Question- What is the importance of curly braces in if-else statements?



Answer-

Importance ofCurly Braces {} in if-else Statements

Curly braces {} play a crucial role inif-else statements because they define the scope of the code block thatshould be executed when a condition is met. Here’s why they are important:

1.    GroupingMultiple Statements:

o   Curly braces allow you to group multiplestatements together under a single if, else, or else-if condition.

o   Without curly braces, only the first statementimmediately following the condition is considered part of the if, else, or else-ifblock.

Example: Java code

int num = 10;

if (num > 5) {

   System.out.println("Number is greater than 5.");

   System.out.println("This is inside the if block.");

}

In this example, both System.out.printlnstatements are executed if the condition is true because they are enclosed incurly braces.

2.    PreventingLogical Errors:

o   Omitting curly braces can lead to logicalerrors, especially when adding or modifying code.

o   If you accidentally add a second statementwithout curly braces, it will not be part of the if or else block, which mightcause unintended behavior.

Example withoutCurly Braces: Java code

int num = 10;

if (num > 5)

   System.out.println("Number is greater than 5.");

   System.out.println("This is outside the if block.");

Here, only the first System.out.printlnstatement is conditionally executed, while the second one will always executeregardless of the condition, which might not be the intended behavior.

3.    Readabilityand Maintenance:

o   Using curly braces improves the readability ofyour code by clearly indicating the start and end of conditional blocks.

o   This clarity is especially helpful whenreviewing or debugging code, as it makes the structure and flow of logic moreapparent.

4.    Consistency:

o   Even though curly braces are technicallyoptional for single-statement blocks, it's a good practice to use themconsistently. This reduces the risk of errors when the code is modified in thefuture.

Summary:

Curly braces {} are essential in if-elsestatements for defining the scope of the code to be executed, preventinglogical errors, improving readability, and maintaining consistent codingpractices.


27

Question- Write a program that checks if a given character is an uppercase letter, lowercase letter, or a digit.



Answer-
import java.util.Scanner;

public class Test
{
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any letter");

char ch=sc.next().charAt(0);
if (ch >= 'A' && ch <= 'Z')
{
    System.out.println("Uppercase letter");
}
else if (ch >= 'a' && ch <= 'z')
{
    System.out.println("Lowercase letter");
}
else if (ch >= '0' && ch <= '9')
{
    System.out.println("Digit");
}
else
{
    System.out.println("Special character");
}
}
}
28

Question- Write a program to check if three angles can form a triangle. A triangle is valid if the sum of its angles is 180 degrees.



Answer-
 //this is the Logic for check if three angles can form a triangle. A triangle is valid if the sum of its angles is 180 degrees.

if (angle1 + angle2 + angle3 == 180)
{
    System.out.println("Valid triangle");
} else {
    System.out.println("Not a valid triangle");
}

29

Question- Write a java program that takes a number and checks if it is divisible by both 5 and 11.



Answer-
 // this is logic for checks if it is divisible by both 5 and 11.

if (number % 5 == 0 && number % 11 == 0)
 {
    System.out.println("Divisible by 5 and 11");
 }
 else
 {
    System.out.println("Not divisible by 5 and 11");
 }

30

Question- Write a Java program to check if a given year is a leap year. A year is a leap year if it's divisible by 4 but not by 100 unless also divisible by 400.



Answer-
 //This is logic for check if a given year is a leap year.

 if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
 {
    System.out.println("Leap year");
 }
 else
 {
    System.out.println("Not a leap year");
 }

31

Question- Write a program that checks if a given date (day, month, year) is valid. Consider leap years and the number of days in each month.



Answer-
 boolean isValid = true;

 if (month < 1 || month > 12)
 {
    isValid = false;
 }
 else if (day < 1 || day > 31)
 {
    isValid = false;
 }
 else if ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30)
 {
    isValid = false;
 }
 else if (month == 2)
 {
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
 {
        if (day > 29) isValid = false;
    }
 else
 {
        if (day > 28) isValid = false;
    }
 }

 System.out.println(isValid ? "Valid Date" : "Invalid Date");

32

Question- Write a program that takes three numbers and checks: 1. If all are equal 2. If all are different 3. If two numbers are equal



Answer-
 // 
if (num1 == num2 && num2 == num3) {
    System.out.println("All numbers are equal");
} else if (num1 != num2 && num1 != num3 && num2 != num3) {
    System.out.println("All numbers are different");
} else {
    System.out.println("Two numbers are equal");
}

33

Question- Write a Java program to categorize age groups: a.) 0-12: Child b.) 13-19: Teen c.) 20-59: Adult d.) 60 and above: Senior



Answer-
 // programming Logic 

 if (age >= 0 && age <= 12)
 {
    System.out.println("Child");
 }
 else if (age >= 13 && age <= 19)
 {
    System.out.println("Teen");
 }
 else if (age >= 20 && age <= 59)
 {
    System.out.println("Adult");
 }
 else if (age >= 60)
 {
    System.out.println("Senior");
 }
 else
 {
    System.out.println("Invalid age");
 }

34

Question- Write a java switch statement that prints if a month belongs to "Winter," "Spring," "Summer," or "Autumn" by combining multiple months in each season.



Answer-
package weekdayswitchquestions;
import java.util.Scanner;
public class WeekdaySwitchQuestions 
{
    public static void main(String[] args) {
         Scanner sc=new Scanner(System.in);
         System.out.println("Enter number of Month");
         int month = sc.nextInt();
        switch (month) {
            case 12, 1, 2 -> System.out.println("Winter");
            case 3, 4, 5 -> System.out.println("Spring");
            case 6, 7, 8 -> System.out.println("Summer");
            case 9, 10, 11 -> System.out.println("Autumn");
            default -> System.out.println("Invalid month");
        }
    }
    
}


35

Question- Write a Java switch expression that takes an integer and returns a string describing if it’s "Low," "Medium," or "High." Use switch arrow syntax.



Answer-

package enterintegerandreturnstring;
 import java.util.Scanner;
 public class EnterIntegerAndReturnString 
 {
    public static void main(String[] args) 
    {
        Scanner sc=new Scanner(System.in);
         System.out.println("Enter number");
         int value = sc.nextInt();
       String range = switch (value) {
            case 1, 2, 3 -> "Low";
            case 4, 5, 6 -> "Medium";
            case 7, 8, 9 -> "High";
            default -> "Unknown";
        };
        System.out.println("Range: " + range);
    }
 }

36

Question- Create an enum for the days of the week, and use a Java switch statement to print whether it’s a weekday or weekend.



Answer-
 enum Day {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
  }

 public class WeekdayChecker {
    public static void main(String[] args) {
        Day day = Day.SATURDAY;
       
        switch (day) {
            case SATURDAY, SUNDAY -> System.out.println("It's a weekend!");
            default -> System.out.println("It's a weekday.");
        
        }
    }
}