ICSE Class 10 Computer Applications Question 20 of 22

Nested for loops — Question 20

Back to all questions
20
Question

Question 9

Write a menu driven program that prompts the user to select one of the four triangle patterns (a, b, c, or d). The program then accepts number of rows and prints the selected pattern as shown below:

Enter the size : 8

(a)

#
# #
# # #
# # # #
# # # # #
# # # # # #
# # # # # # #
# # # # # # # #

(b)

# # # # # # # #
# # # # # # # 
# # # # # # 
# # # # # 
# # # # 
# # # 
# # 
#

(c)

# # # # # # # #
  # # # # # # # 
    # # # # # # 
      # # # # # 
        # # # # 
          # # # 
            # # 
              #

(d)

              #
            # #
          # # #
        # # # #
      # # # # #
    # # # # # #
  # # # # # # # 
# # # # # # # #
import java.util.Scanner;

public class KboatTriangleChoice
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Type a for Pattern a");
        System.out.println("Type b for Pattern b");
        System.out.println("Type c for Pattern c");
        System.out.println("Type d for Pattern d");
        
        System.out.print("Enter your choice: ");
        char ch = in.next().charAt(0);
        
        System.out.print("Enter the number of terms: ");
        int n = in.nextInt();
        
        switch (ch) {
            case 'a':
                for (int i = 1; i <= n; i++) { 
                    for (int j = 1; j <= i; j++) 
                        System.out.print('#');
                    
                    System.out.println();
                }
                break;
            
            case 'b': 
                for (int i = n; i >= 1; i--) {   
                    for (int j = 1; j <= i; j++) 
                        System.out.print('#');
                    
                    System.out.println();
                }
                break;
            
            case 'c': 
                for (int i = n; i >= 1; i--) {
                    for (int j = i; j < n; j++) 
                        System.out.print(" ");
                    for (int k = 0; k < i; k++) 
                        System.out.print('#');
                    System.out.println();
                }
                break;
                
            case 'd': 
                for (int i = n; i >= 1; i--) {
                    for (int j = 1; j < i; j++)
                        System.out.print(" ");
                    for (int k = i; k <= n; k++)
                        System.out.print('#');
                    System.out.println();
                }
                break;
            default:
                System.out.println("Incorrect choice");
                break;
        }
    }
}
Output
BlueJ output of KboatTriangleChoice.java
BlueJ output of KboatTriangleChoice.java
BlueJ output of KboatTriangleChoice.java
BlueJ output of KboatTriangleChoice.java
Answer

Source: This question is from Nested for loops, Computer Applications — Class 10, ICSE Board.

Key Concepts Covered

This question tests your understanding of the following concepts from the chapter Nested for loops: Question, Menu, Driven, Program, Prompts, User. These are fundamental topics in Computer Applications that students are expected to master as part of the ICSE Class 10 curriculum.

A thorough understanding of these concepts will help you answer similar questions confidently in your ICSE examinations. These topics are frequently tested in both objective and subjective sections of Computer Applications papers. We recommend revising the relevant section of your textbook alongside practising these solved examples to build a strong foundation.

How to Approach This Question

Read the question carefully and identify what is being asked. Break down complex questions into smaller parts. Use the terminology and concepts discussed in this chapter. Structure your answer logically — begin with a definition or key statement, then provide supporting details. Review your answer to ensure it addresses all parts of the question completely.

Key Points to Remember

  • Write programs with proper indentation and comments.
  • Trace through your code with sample inputs to verify correctness.
  • Explain the logic behind each step of your solution.
  • Familiarise yourself with common library functions and methods.

Practice more questions from Nested for loops — Computer Applications, Class 10 ICSE