IT Developer

Nested for Loops in Java

Chapter 10

Nested for Loops in Java

Class 10 - Logix Kips ICSE Computer Applications with BlueJ


Share with a Friend

Java Program: Menu Driven Program to Display Triangle Patterns


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)


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

Java Program : Menu Driven Program to Display Triangle Patterns

import java.util.Scanner;

 

public class TrianglePatterns

{

    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

Sample Output 1 : 
Type a for Pattern a
Type b for Pattern b
Type c for Pattern c
Type d for Pattern d
Enter your choice: a
Enter the number of terms: 8
#
##
###
####
#####
######
#######
########

Sample Output 2 : 
Type a for Pattern a
Type b for Pattern b
Type c for Pattern c
Type d for Pattern d
Enter your choice: b
Enter the number of terms: 8
########
#######
######
#####
####
###
##
#

Sample Output 3 : 
Type a for Pattern a
Type b for Pattern b
Type c for Pattern c
Type d for Pattern d
Enter your choice: c
Enter the number of terms: 8
 
 # # # # # # # #  
    # # # # # # #  
       # # # # # #  
          # # # # #  
             # # # # 
                # # # 
                   # # 
                     #  

Sample Output 4 : 
Type a for Pattern a
Type b for Pattern b
Type c for Pattern c
Type d for Pattern d
Enter your choice: d
Enter the number of terms: 8
 
                      # 
                   # # 
                # # #
             # # # # 
          # # # # # 
       # # # # # # 
    # # # # # # # 
 # # # # # # # # 

📝 Explanation