img

Switch Case: Solved Practice Questions For Beginners

This is the solution for the practice question set on the concepts of switch case and fall through. You can find the corresponding list of questions here. Try solving the questions yourself before you go through the solutions.


Grade Classification Based on Marks

 

Problem: Write a program to classify a student’s grade based on marks using a `switch` statement. The program should convert the student's marks to a grade letter:

     - 90-100: "A"

     - 80-89: "B"

     - 70-79: "C"

     - 60-69: "D"

     - Below 60: "Fail"

Condition: To handle ranges in a `switch` statement, you can first convert marks to tens (e.g., 87 becomes 8) and use this in the `switch` case. Print the corresponding grade for each range.


CODE : 


import java.io.*;

import java.util.*;


class Main

{

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        //declaring all variables

        int marks,tens;

        //taking the marks as input

        System.out.print("Enter the student's marks : ");

        marks=sc.nextInt();

        tens=marks/10;

        System.out.print("The grade is ");

        //performing calculations

        switch(tens)

        {

            case 9:

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

                break;

            case 8:

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

                break;

            case 7:

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

                break;

            case 6:

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

                break;

            default:

                System.out.println("Fail.");

                break;

        }

    }

}


Day of the Week Finder


Problem: Write a program that takes a number between 1 and 7 as input (e.g., 1 = Monday, 7 = Sunday) and displays whether it is a Weekday or a Weekend using the concept of `fall through` in switch.


CODE : 


import java.io.*;

import java.util.*;


class Main

{

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        //declaring all variables

        int day;

        //taking the day number as input

        System.out.println("For 1 = Monday and 7 = Sunday...");

        System.out.print("Enter the day number : ");

        day=sc.nextInt();

        System.out.print("The day is a ");

        //performing calculations

        switch(day)

        {

            case 1:

            case 2:

            case 3:

            case 4:

            case 5:

                System.out.println("weekday.");

                break;

            case 6:

            case 7:

                System.out.println("weekend.");

                break;

            default:

                System.out.println("Invalid Input.");

                break;

        }

    }

}


Vending Machine Simulator

 

Problem: Create a vending machine simulator that displays options for beverages:

     - 1: Coffee - ₹20

     - 2: Tea - ₹15

     - 3: Juice - ₹30

     - 4: Water - ₹5

Condition: Use a `switch` statement to take the user’s choice. Then take the number of beverages required as input and print the final bill. If the user selects an invalid option, print "Invalid selection."


CODE : 


import java.io.*;

import java.util.*;


class Main

{

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        //declaring all variables

        int choice, count;

        //taking the choice as input

        System.out.println("1: Coffee - Rs 20");

        System.out.println("2: Tea - Rs 15");

        System.out.println("3: Juice - Rs 30");

        System.out.println("4: Water - Rs 5");

        System.out.print("Enter the user's choice : ");

        choice=sc.nextInt();

        //taking the count of beverages required as input

        System.out.print("Enter the no. of beverages required : ");

        count=sc.nextInt();

        System.out.print("The final bill is Rs ");

        //performing calculations

        switch(choice)

        {

            case 1:

                System.out.println(20*count);

                break;

            case 2:

                System.out.println(15*count);

                break;

            case 3:

                System.out.println(30*count);

                break;

            case 4:

                System.out.println(5*count);

                break;

            default:

                System.out.println("Invalid selection.");

                break;

        }

    }

}


Leap Year Checker Based on the Month and Year


Problem: Write a program that takes a month (1-12) and year as input and determines the number of days in that month, using the concept of fall through in `switch` statement. 

Conditions:

     - For February (month = 2), if it's a leap year, print "29 days"; otherwise, print "28 days."

     - For months with fixed days (April, June, September, and November), print "30 days."

     - For other months, print "31 days."

Use the `switch` statement to handle each month's case and apply an additional check for February.


CODE : 


import java.io.*;

import java.util.*;


class Main

{

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        //declaring all variables

        int month,year;

        //taking the month and year as input

        System.out.print("Enter the month number : ");

        month=sc.nextInt();

        System.out.print("Enter the year : ");

        year=sc.nextInt();

        System.out.print("The month has : ");

        //performing calculations

        switch(month)

        {

            case 1:

            case 3:

            case 5:

            case 7:

            case 8:

            case 10:

            case 12:

                System.out.println("31 Days.");

                break;

            case 4:

            case 6:

            case 9:

            case 11:

                System.out.println("30 Days.");

                break;

            case 2:

                if(year%4==0 && year%100!=0 || year%400==0)

                {

                    System.out.println("29 Days.");

                }

                else

                {

                    System.out.println("28 Days.");

                }

                break;

            default:

                System.out.println("Invalid Input.");

                break;

        }

    }

}


Simple Calculator 


Problem: Create a simple calculator using a `switch` case to handle different operations based on a menu. The menu should display:

     - 1: Addition

     - 2: Subtraction

     - 3: Multiplication

     - 4: Division

Conditions:

After the user chooses an option, prompt them to enter two numbers and perform the selected operation. Include a default case that says "Invalid operation, please try again."


CODE : 


import java.io.*;

import java.util.*;


class Main

{

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        //declaring all variables

        int choice, num1, num2;

        //taking the choice as input

        System.out.println("1: Addition");

        System.out.println("2: Subtraction");

        System.out.println("3: Multiplication");

        System.out.println("4: Division");

        System.out.println("5: Modulus");

        System.out.print("Enter the user's choice : ");

        choice=sc.nextInt();

        //taking the numbers required as input

        System.out.print("Enter the first number : ");

        num1=sc.nextInt();

        System.out.print("Enter the second number : ");

        num2=sc.nextInt();

        //performing calculations

        switch(choice)

        {

            case 1:

                System.out.println("Sum : "+(num1+num2));

                break;

            case 2:

                System.out.println("Difference : "+(num1-num2));

                break;

            case 3:

                System.out.println("Product : "+(num1*num2));

                break;

            case 4:

                System.out.println("Quotient : "+(num1/num2));

                break;

            case 5:

                System.out.println("Remainder : "+(num1%num2));

                break;

            default:

                System.out.println("Invalid selection.");

                break;

        }

    }

}

post-img

Mindset Shifts To Rewire How You Work An...

A collection of mindset shifts that act as gentle rewirings of how we approach productivity and cultivate meaningful impact

Read More

post-img

Essential Steps to Build a Solid Foundat...

Master core programming concepts like loops, strings, and arrays with curated practice questions.

Read More

post-img

Pattern Programs In Java

This is the solution for the practice question set on the concept of star and number patterns in programming languages.

Read More

philomathiq

© 2023 philomathiq. All rights reserved.