Java Program to Print Pattern Triangle

By |2017-04-09T08:07:27+05:30June 21st, 2016|java programs|

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

Java Program to Print Pattern Triangle :


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();
        }
    }
}

Triange Numbers Output

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment