In this tutorial, we will write a program to print a triangle pattern in java where the pattern is repeated on each row and keep on increasing. Before that, you may go through the following topic in java.
Example:
Input: 5
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
The program takes the user input for the number of rows and displays the result using the for loop in java. The program is mirror right angled triangle pattern with a repetition pattern or you can say half pyramid of numbers with a repetition pattern.
Java Program to print the right triangle number pattern
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import java.util.Scanner; public class Main { public static void main(String[] args) { int rows; Scanner sc = new Scanner(System.in); System.out.print("Enter the number of rows: "); rows = sc.nextInt(); for (int i = 1; i <= rows; i++) { //first half of the row for (int j = 1; j <= i; j++) System.out.print(j + " "); //second half of the row for (int j = i - 1; j >= 1; j--) System.out.print(j + " "); System.out.println(); } } } |
Output:
Enter the number of rows: 5
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 11 2 3 4 5 6 5 4 3 2 1