Switch-Case

 switch-case statement

1.      Using the switch-case statement, write a menu-driven program to do the following:

         

(a) To generate and print letters from A to Z and their Unicode.

Letters    Unicode

A          65

B          66

.          .

.          .

.          .

Z          90

 

(b) Display the following pattern using iteration (looping) statement:

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

 

SOLUTION

 

import java.util.Scanner;

class MenuDriven

{

    public static void main(String args[])

    {

        Scanner sc = new Scanner(System.in);

        System.out.println("1. Print Unicode Values");

        System.out.println("2. Traingle Pattern");

        System.out.println("Please enter your choice: ");

        int choice = sc.nextInt();

        switch(choice)

        {

            case 1:

            System.out.println("Letters\t\tUnicode");

            for(char p = 'A'; p <= 'Z'; p++)

            {

                System.out.println(p + "\t" + (int)p);

            }

            break;

            case 2:

            for(int i = 1; i <= 5; i++)

            {

                for(int j = 1; j <= i; j++)

                {

                    System.out.print(j + " ");

                }

                System.out.println();

            }

            break;

            default:

            System.out.println("Wrong choice!");

        }

    }

}

NOTE: This question is also a part of pattern section

       

 

2.      Write a menu-driven program to display the pattern as per user’s choice:

 

Pattern 1                               Pattern 2

ABCDE                                B

ABCD                                  LL

ABC                                     UUU

AB                                        EEEE

A

For an incorrect option, an appropriate error message should be displayed.

 

SOLUTION

               

import java.util.Scanner;

public class MenuDriven

 {

 

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Please enter your choice: 1. ABCDE pattern 2. BLUE pattern ");

 

        int choice = sc.nextInt();

switch(choice)

        {

case 1:

           

      int rows=5;

            for (int i = 1; i<=n; i++) {

        int k=65;// 65 is the ASCII code of capital A

                for (int j = 1; j <= (n-i+1); j++) {

                    System.out.print((char)k);//explicit typecasting

          k++;

                }

                System.out.println();

            }

      break;

        case 2:

           

            String s = "BLUE";

            for (int i = 0; i < s.length(); i++) {

                char ch = s.charAt(i);

                for (int j = 1; j <= (i + 1); j++) {

                    System.out.print(ch);

                }

                System.out.println();

      }

      break;

            default:

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

        }

 

    }

}

NOTE: This question is also a part of pattern section

 

 

3.      Using switch statement, write a menu-driven program for the following:

 

(i) To find and display the sum of the series given below:

S = x1 – x2 + x3 – x4 + x5 … – x20, where x = 2.

 

(ii) To display the following series:

1 11 111 1111 11111

 

For an incorrect option, an appropriate error message should be displayed

 

SOLUTION

 

import java.util.Scanner;

public class MenuDriven

{

  public static void main(String[] args)

  {  

    System.out.println("Please Enter your choice: ");

    System.out.println("1. To Display Sum of series");

    System.out.println("2. To Display Series");

        Scanner sc = new Scanner(System.in);

    int choice = sc.nextInt();

    switch (choice)

    {

    case 1:

      double sum = 0.0,x=2.0;

      for (int i = 1; i <= 20; i++)

      {

        if (i % 2 == 1)

        {

          sum = sum + Math.pow(x, i);

        }

        else

        {

          sum = sum - Math.pow(x, i);

        }

      }

      System.out.println("Sum of series = " + sum);

      break;

    case 2:

      for (int i = 1; i <= 5; i++)

      {

        for (int j = 1; j <= i; j++)

        {

          System.out.print("1");

        }

        System.out.print(" "); //for same line no sopln required

      }

      break;

    default:

      System.out.println("Wrong choice");

      break;

    }

  }

}

 

 

4.      Using the switch statement, write a menu-driven program for the following:

 

(i) To print the Floyd’s triangle given below:

1

2 3

4 5 6

7 8 9 10

11 12 13 14 15

 

(ii) To display the following pattern:

I

IC

ICS

ICSE

 

For an incorrect option, an appropriate error message should be displayed.

 

SOLUTION

               

import java.util.Scanner;

class MenuDriven2016

{

    public static void main(String args[])

    {

        Scanner sc = new Scanner(System.in);

        System.out.println("Please Enter your choice: ");

        System.out.println("1. Floyd's Triangle pattern");

        System.out.print("2. ICSE Pattern");

       

        int ch = sc.nextInt();

        switch(ch)

        {

            case 1:

            int digit = 1;

            for(int i = 1; i <= 5; i++){

                for(int j = 1; j <= i; j++){

                    System.out.print(digit + " ");

                    digit++;

                }

                System.out.println();

            }

            break;

            case 2:

            String s = "ICSE";

            for(int i = 0; i< s.length(); i++)

            {

                for(int j = 0; j <= i; j++)

                {

                    System.out.print(s.charAt(j) + " ");

                   

                }

                System.out.println();

            }

            break;

            default:

            System.out.println("Invalid choice!");

        }

    }

}

NOTE: This question is also a part of pattern section

 

         

 

5.      Using the switch statement, write a menu-driven program to:

 

(i) To find and display all the factors of a number input by the user (including 1 and excluding the number itself).

 

Example:

INPUT:

n = 15

OUTPUT:

1, 3, 5

 

(ii) To find and display the factorial of a number input by the user. The factorial of a non-negative integer n, denoted by n!, is the product of all integers less than or equal to n.

 

Example:

INPUT:

n = 5

OUTPUT:

5! = 1 × 2 × 3 × 4 × 5 = 120.

 

For an incorrect choice, an appropriate error message should be displayed.

 

SOLUTION               

 

import java.util.Scanner;

class MenuDriven2015

{

    public static void main(String args[])

    {

        Scanner sc = new Scanner(System.in);

        System.out.println("Please Enter your choice: ");

        System.out.println("1. Factors");

        System.out.println("2. Factorial");

       

        int ch = sc.nextInt();

        switch(ch)

        {

            case 1:

            System.out.print("Please enter any Number: ");

            int n = sc.nextInt();

            for(int i = 1; i < n; i++)

            {

                if(n % i == 0)

                    System.out.print(i + " ");

            }

            break;

            case 2:

            System.out.print("Please enter any Number: ");

            n =sc.nextInt();

            int fact = 1;

            for(int i = 1; i <= n; i++)

                fact *= i; //fact=fact*i;

            System.out.println("factorial is :" +fact);

            break;

            default:

            System.out.println("Invalid choice!");

        }

    }

}

         

 

6.      Using the switch statement, write a menu-driven program to calculate the maturity amount of a bank deposit.

 

The user is given the following options:

(i) Term Deposit

(ii) Recurring Deposit

 

For option (i) accept Principal (p), rate of interest (r) and time period in years (n). Calculate and output the maturity amount (a) receivable using the formula a = p[1 + r / 100]n.

For option (ii) accept monthly installment (p), rate of interest (r) and time period in months (n). Calculate and output the maturity amount (a) receivable using the formula a = p * n + p * n(n + 1) / 2 * r / 100 * 1 / 12.

For an incorrect option, an appropriate error message should be displayed.

 

SOLUTION               

 

import java.util.Scanner;

class MenuDriven2014

{

    public static void main(String args[])

    {

        Scanner sc = new Scanner(System.in);

        System.out.println("Please enter your choice: ");

        System.out.println("1. Term Deposit");

        System.out.println("2. Recurring Deposit");

        double a=0,p=0,r=0,n=0;

        int ch = sc.nextInt();

 

        switch(ch)

        {

            case 1:

            System.out.println("Please Enter Principal: ");

             p = sc.nextDouble();

            System.out.println("Please enter rate of interest: ");

             r = sc.nextDouble();

            System.out.println("Please enter time in years: ");

             n = sc.nextDouble();

             a = p * Math.pow(1 + r / 100, n);

            System.out.println("Maturity amount is  " + a);

            break;

            case 2:

            System.out.println("Please enter Monthly installment: ");

            p =  sc.nextDouble();

            System.out.println("Please enter Rate of interest: ");

            r =  sc.nextDouble();

            System.out.println("Please enter Time in months: ");

            n =  sc.nextDouble();

            a = p * n + p * (n * (n + 1) / 2) * (r / 100) * (1.0 / 12);

            System.out.println("Maturity amount is " + a);

            break;

            default:

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

        }

    }

}

         

 

7.      Using the switch statement, write a menu-driven program:

 

(i) To check and display whether a number input by the user is a composite number or not.  (A number is said to be composite if it has one or more than one factor excluding 1 and the number itself.). Example: 4, 6, 8, 9, …

 

(ii) To find the smallest digit of an integer that is input:

Sample Input: 6524

Output: Smallest digit is 2.

 

For an incorrect choice, an appropriate error message should be displayed.

 

SOLUTION

               

import java.util.Scanner;

 

class MenuDriven

{

    public static void main(String args[])

    {

        Scanner sc = new Scanner(System.in);

        System.out.println("Please enter your choice: ");

        System.out.println("1. Composite Number");

        System.out.println("2. Smallest Digit");

        int n=0;

        int ch = sc.nextInt();

        switch(ch){

            case 1:

            System.out.print("Please enter any number ");

            n = sc.nextInt();

            int c = 0;

            for(int i = 1; i <= n; i++){

                if(n % i == 0)

                    c++;

            }

            if(c > 2)

                System.out.println(n + " is  a composite number");

            else

                System.out.println(n + " is not a composite number");

            break;

            case 2:

            System.out.print("Please enter any number: ");

            n = sc.nextInt();

            int min = n % 10;

            for(int i = n; i>0;i=i/10)

                {

                int r = i % 10;

                if(min > r)

                    min = r;

                }

            System.out.println(min + " is the smallest digit");

            break;

            default:

            System.out.println("Invalid choice!");

        }

    }

}  

 

 

8.      Using the switch statement, write a menu-driven program to:

 

(i) Generate and display the first 10 terms of the Fibonacci series 0, 1, 1, 2, 3, 5, …

The first two Fibonacci numbers are 0 and 1, and each subsequent number is the sum of the previous two.

 

(ii) Find the sum of the digits of an integer that is input.

 

Sample Input: 15390

Sample Output: Sum of the digits = 18.

For an incorrect choice, an appropriate error message should be displayed.

 

SOLUTION

            

import java.util.Scanner;

class MenuDriven2012

{

    public static void main(String args[])

    {

        Scanner sc = new Scanner(System.in);

        System.out.print("Please Enter your choice: ");

        System.out.println("1. First 10 Fibonacci terms");

        System.out.println("2. Sum of the digits");

       

        int ch = sc.nextInt();

        switch(ch){

            case 1:

            int a = 0;

            int b = 1;

            System.out.print(a + " " + b);

            for(int i = 3; i <= 10; i++){

                c=a+b;

                System.out.print(" " + c);

                a = b;

                b = c;

            }

            break;

            case 2:

            System.out.print("Please enter any digit: ");

            int n = sc.nextInt();

            int sum = 0,r=0;

            for(if i=n;i>0;i=i/10)

            {

                r=i%10;

                sum=sum+r;

 

            }

            System.out.println("Sum of the digits: " + sum);

            break;

            default:

            System.out.println("Invalid choice!");

        }

    }

}

          

 

9.      Write a menu-driven program to perform the following (use switch case statement):

 

(a) To print the series 0, 3 7, 15, 24, … n terms (value of ‘n’ is to be an input by the user)

 

(b) To find the sum of the series given below: S = 1 / 2 + 3 / 4 + 5 / 6 + 7 / 8 + … + 19 / 20.

 

SOLUTION               

 

import java.util.Scanner;

class MenuDriven2011{

    public static void main(String args[])

    {

        Scanner sc = new Scanner(System.in);

        System.out.print("Please Enter your choice: ");

        System.out.println("1. to ptint series 1");

        System.out.println("2. To calculate sum of given series 2");

        System.out.print("Enter your choice: ");

        int ch = sc.nextInt();

        switch(ch)

        {

            case 1:

            System.out.print("Please enter any value ");

            int n = sc.nextInt();

            for(int i = 1; i <= n; i++)

                System.out.print((i * i - 1) + " ");

            break;

            case 2:

            double sum = 0.0;

            for(double i = 1; i <= 19; i++)

                sum = sum + i / (i + 1);

            System.out.print("Sum of the series is " + sum);

            break;

            default:

            System.out.println("Invalid choice!");

        }

    }

}

 

 

10.  Write a menu-driven program to accept a number and check and display whether it is a prime number or not, or an automorphic number or not. Use switch-case statement.

 

(a) Prime number: A number is said to be a prime number if it is divisible only by 1 and itself and not by any other number.

Example: 3, 5, 7, 11, 13, etc.

 

(b) Automorphic number: An automorphic number is the number which is contained in the last digit(s) of its square.

Example: 25 is an automorphic number as its square is 625 and 25 is present as the last two digits.

 

SOLUTION

               

import java.util.Scanner;

class MenuDriven2010

{

    public static void main(String args[])

    {

        Scanner sc = new Scanner(System.in);

        System.out.println("ENTER YOUR CHOICE");

        System.out.println("1. Prime Number");

        System.out.println("2. Automorphic Number");

         int n=0,count=0;

        int ch = sc.nextInt();

        switch(ch){

            case 1:

            System.out.println("Enter any value ");

            n = sc.nextInt();

            count = 0;

            for(int i = 1; i <= n; i++)

            {

                if(n % i == 0)

                    count++;

            }

            if(count == 2)

                System.out.println(n + " is a prime number");

            else

                System.out.println(n + " is not a prime number");

            break;

 

            case 2:

            System.out.println("Enter any value ");

            n = sc.nextInt();

           

            int square = n * n;

            for(int i = n;i>0;i = i/10) //Digit Counting

                count++;

           

            int last= square % (int)Math.pow(10,count);//Extract last digits to check   

           

            if(last==n)   

                System.out.println(n + " is an automorphic number");

            else

                System.out.println(n + " is not an automorphic number");

            break;

           

            default:

            System.out.println("Wrong choice");

        }

    }

}

 

No comments:

Post a Comment