Let’s see java program to print pattern of asterisks(‘*’) based on a given number of rows. The program should be prompt the users to enter the number of rows,and then display a pyramid pattern accorndingly. 

Pattern in java

				
					import java.util.Scanner;

public class TrianglePattern {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the number of rows: ");
        int rows = scanner.nextInt();
        
        for (int i = 1; i <= rows; i++) {
            // Print spaces
            for (int j = 1; j <= rows - i; j++) {
                System.out.print(" ");
            }
            
            // Print stars
            for (int k = 1; k <= i; k++) {
                System.out.print("* ");
            }
            
            // Move to the next line
            System.out.println();
        }
        
        scanner.close();
    }
}

				
			

This program prompts the user to enter the number of rows for the triangle and then prints a triangle pattern with that number of rows using asterisks (*) and spaces. The number of spaces decreases from the first row to the last row, while the number of asterisks increases from the first row to the last row.

For example, if the user enters 5, the output will be:

Out Put

prime number

Let’s go through the code step by step:

1. **Import Statements**: The program imports the Scanner class from the java.util package to allow user input.

2. **Class Declaration**: A class named `TrianglePattern` is declared.

3. **Main Method**: The main method is the entry point of the program.

4. **User Input**: The program prompts the user to enter the number of rows for the triangle pattern.

5. **Reading Input**: The number of rows entered by the user is read and stored in the variable `rows`.

6. **Outer Loop (Row Loop)**: The outer loop (`for` loop) is responsible for iterating over each row of the triangle pattern. It starts from 1 and goes up to the number of rows entered by the user.

7. **Inner Loop (Space Loop)**: Inside the outer loop, there is an inner loop (`for` loop) responsible for printing spaces before the stars in each row. The number of spaces decreases as we move down the rows, which is achieved by printing a number of spaces equal to `(rows – i)`.

8. **Inner Loop (Star Loop)**: After printing the spaces, another inner loop is used to print the stars (`*`). The number of stars increases as we move down the rows, which is achieved by printing a number of stars equal to the current row number (`i`).

9. **Print New Line**: After printing the spaces and stars for each row, the program moves to the next line to start printing the next row.

10. **Closing Scanner**: Finally, the Scanner object is closed to prevent resource leaks.

This program generates a triangle pattern with the number of rows specified by the user, where each row has a different number of stars and spaces. Adjustments can be made to the loops and symbols to create various other patterns.

 
Scroll to Top