Java Program to Print Triangle Numbers Pattern

Write a Java program to print triangle numbers pattern using for loop.

package Shapes3;

import java.util.Scanner;

public class TriangleNum1 {

	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Enter Triangle Number Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Printing Triangle Number Pattern");
		
		for (int i = 1; i <= rows; i++ ) 
		{
			for (int j = rows; j > i; j-- ) 
			{
				System.out.print(" ");
			}
			for(int k = 1; k <= i; k++) 
			{
				System.out.print(k + " ");
			}
			System.out.println();
		}
	}
}
Java Program to Print Triangle Numbers Pattern

This Java program prints the triangle pattern of numbers using while loop.

package Shapes3;

import java.util.Scanner;

public class TriangleNum2 {

	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Enter Triangle Number Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Printing Triangle Number Pattern");
		int j, k, i = 1;
		
		while( i <= rows ) 
		{
			j = rows;
			while(j > i ) 
			{
				System.out.print(" ");
				j--;
			}
			
			k = 1;
			while(k <= i) 
			{
				System.out.print(k + " ");
				k++;
			}
			System.out.println();
			i++;
		}
	}
}
Enter Triangle Number Pattern Rows = 8
Printing Triangle Number Pattern
       1 
      1 2 
     1 2 3 
    1 2 3 4 
   1 2 3 4 5 
  1 2 3 4 5 6 
 1 2 3 4 5 6 7 
1 2 3 4 5 6 7 8 

This Java example uses the do while loop to display the triangle of numbers pattern.

package Shapes3;

import java.util.Scanner;

public class TriangleNum3 {

	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Enter Triangle Number Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Printing Triangle Number Pattern");
		int j, k, i = 1;
		
		do 
		{
			j = rows;
			do 
			{
				System.out.print(" ");

			} while(j-- > i );
			
			k = 1;
			do
			{
				System.out.print(k + " ");
	
			} while(++k <= i);
			System.out.println();

		} while(++i <= rows );
	}
}
Enter Triangle Number Pattern Rows = 9
Printing Triangle Number Pattern
         1 
        1 2 
       1 2 3 
      1 2 3 4 
     1 2 3 4 5 
    1 2 3 4 5 6 
   1 2 3 4 5 6 7 
  1 2 3 4 5 6 7 8 
 1 2 3 4 5 6 7 8 9