KnowledgeBoat Logo

Computer Applications

Using switch-case statement write a menu driven program for the following:

  1. To display the pattern given below:
    A
    B C
    D E F
    G H I J
    K L M N O

  2. To display the sum of the series given below:
    1/1! + 3/3! + 5/5! + ….. + n/n!

Java

Java Nested for Loops

17 Likes

Answer

import java.util.Scanner;

public class KboatMenu
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Type 1 for Pattern");
        System.out.println("Type 2 for Series");
        System.out.print("Enter your choice: ");
        int choice = in.nextInt();
        switch (choice) {
            case 1:
            char ch = 'A';
            for (int i = 1; i <= 5; i++) {
                for (int j = 1; j <= i; j++) {
                    System.out.print(ch + " ");
                    ch++;
                }
                System.out.println();
            }
            break;

            case 2:
            System.out.print("Enter n: ");
            int n = in.nextInt();
            double s = 0;
            for (int i = 1; i <= n; i++) {
                double f = 1;
                for (int j = 1; j <= i; j++) {
                    f *= j;
                }
                double t = i / f;
                s += t;
            }
            System.out.println("Series Sum = " + s);
            break;

            default:
            System.out.println("Incorrect Choice");
        }
    }
}

Output

BlueJ output of Using switch-case statement write a menu driven program for the following: (a) To display the pattern given below: A B C D E F G H I J K L M N O (b) To display the sum of the series given below: 1/1! + 3/3! + 5/5! + ….. + n/n!BlueJ output of Using switch-case statement write a menu driven program for the following: (a) To display the pattern given below: A B C D E F G H I J K L M N O (b) To display the sum of the series given below: 1/1! + 3/3! + 5/5! + ….. + n/n!

Answered By

5 Likes


Related Questions