KnowledgeBoat Logo

Computer Applications

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)

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6 
1 2 3 4 5 6 7 
1 2 3 4 5 6 7 8

(b)

1 2 3 4 5 6 7 8
  1 2 3 4 5 6 7 
    1 2 3 4 5 6 
      1 2 3 4 5 
        1 2 3 4 
          1 2 3 
            1 2 
              1

(c)

              1
            1 2
          1 2 3
        1 2 3 4 
      1 2 3 4 5 
    1 2 3 4 5 6
  1 2 3 4 5 6 7
1 2 3 4 5 6 7 8

(d)

1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 
1 2 3 4 5 6 
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1

Java

Java Nested for Loops

4 Likes

Answer

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(j);
                    System.out.println();
                }
                break;
            
            case 'b': 
                for (int i = n; i >= 1; i--)    {   
                    for (int j = i; j < n; j++) 
                        System.out.print(" ");    
                    for (int k = 1; k <= i; k++)
                        System.out.print(k);    
                    System.out.println();
                }
                break;
            
            case 'c': 
                for (int i = 1; i <= n; i++) {
                    for (int j = i; j < n; j++)
                        System.out.print(" ");
                    for (int k = 1; k <= i; k++)
                        System.out.print(k);
                    
                    System.out.println();
                }
                break;
                
            case 'd': 
                for (int i = n; i >= 1; i--) {
                    for (int j = 1; j <= i; j++) 
                        System.out.print(j);
                    System.out.println();
                }
                break;
            default:
                System.out.println("Incorrect choice");
                break;
        }
    }
}

Output

BlueJ output of 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) 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 (b) 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 (c) 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 (d) 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1BlueJ output of 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) 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 (b) 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 (c) 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 (d) 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1BlueJ output of 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) 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 (b) 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 (c) 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 (d) 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1BlueJ output of 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) 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 (b) 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 (c) 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 (d) 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1

Answered By

2 Likes


Related Questions