- Java program to print number triangle pattern five using for loop.
Sample Output,
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Java program to print number triangle pattern five
package com.tcc.java.programs;
public class PatternFour {
public static void main(String[] arg) {
int rows, i, j;
rows = 5;
// Printing upper triangle
/* Outer for loop prints one row in every iteration */
for (i = 1; i <= rows; i++) {
/* Inner for loop prints numbers of one row */
for (j = 1; j <= rows - i + 1; j++) {
System.out.print(j + " ");
}
System.out.print("\n");
}
// Printing Lower Triangle
/* Outer for loop prints one row in every iteration */
for (i = 2; i <= rows; i++) {
/* Inner for loop prints numbers of one row */
for (j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.print("\n");
}
}
}
Output
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5