Java Programs

Java Program to Print Pattern Triangle

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

Found a mistake or something out of date? Tell us →

Written by

chandrashekhar

Founder of onlinetutorialspoint.com. Enterprise architect with 15+ years in healthcare technology, designing and running systems on AWS and Azure. Writes about building real things with AI, local AI hardware and cloud certifications.

Keep reading

Ask a question or leave a comment