Java Program to Print Number Triangle Pattern

  • Java program to print number triangle pattern using for loop.
Sample Pattern,
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 
1 2 
1 

Java program to print number triangle pattern

package com.tcc.java.programs;

public class TrianglePatternThree {
    public static void main(String[] arg) {
        int i, j, c = 0;
        int rows = 7;
        // Printing Upper half of pattern
        // Outer for loop prints one row in every iteration
        for (i = 0; i < rows / 2 + 1; i++) {
            /* Inner for loop prints numbers of one row */
            for (j = 1; j <= i + 1; j++) {
                System.out.print(j + " ");
            }
            System.out.print("\n");
        }

        // Printing lower half of pattern
        // Outer for loop prints one row in every iteration
        for (i = 0; i <= rows / 2; i++) {
            /* Inner for loop prints numbers of one row */
            for (j = 1; j <= rows / 2 - i; j++) {
                System.out.print(j + " ");
            }
            System.out.print("\n");
        }
    }
}
Output
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 
1 2 
1