Here we are going to write a program to print like below triangle numbers.

Java Program to Print Pattern Triangle :

[java]

package com.onlinetutorialspoint.patterns;

import java.util.Scanner;

public class Trangle_Numbers {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the size of Triangle");
        int size = scanner.nextInt();
        int cols = 1;
        int spaces = size – 1;
        for (int i = 1; i < size; i++) {
            for (int j = 1; j < spaces; j++) {
                System.out.print(" ");
            }
            int middle = 0;
            for (int j = 1; j <= cols; j++) {
                if (i >= j) {
                    middle = middle + 1;
                } else {
                    middle = middle – 1;
                }
                System.out.print(middle);
            }
            cols = cols + 2;
            spaces = spaces – 1;
            System.out.println();
        }
    }
}

[/java]

Triange Numbers Output

About the Author:

Founder of onlinetutorialspoint.com Love Java, Python, Shell and opensource frameworks. Follow him on twitter and facebook for latest updates.

Leave A Comment