Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: ICSE Java Programming Function Overload 2016 Q5 Solved

Tuesday, September 19, 2023

ICSE Java Programming Function Overload 2016 Q5 Solved

Using the switch statement, write a menu driven program in Java 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 I C I C S I C S E  

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



import java.util.Scanner;

public class PatternCreation {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int choice;

        do {
            System.out.println("Menu:");
            System.out.println("1. Print Floyd's Triangle");
            System.out.println("2. Display Pattern");
            System.out.println("3. Exit");
            System.out.print("Enter your choice (1/2/3): ");
            choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    printFloydsTriangle();
                    break;
                case 2:
                    displayPattern();
                    break;
                case 3:
                    System.out.println("Exiting the program.");
                    break;
                default:
                    System.out.println("Invalid choice. Please select 1, 2, or 3.");
                    break;
            }
        } while (choice != 3);

        scanner.close();
    }

    // Method to print Floyd's Triangle
    public static void printFloydsTriangle() {
        int n = 1;
        int rows = 5; // You can change the number of rows as needed

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(n + " ");
                n++;
            }
            System.out.println();
        }
    }

    // Method to display the specified pattern
    public static void displayPattern() {
        String pattern = "ICSE";
        for (int i = 0; i < pattern.length(); i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print(pattern.charAt(j) + " ");
            }
            System.out.println();
        }
    }
}

No comments:

Post a Comment