Open In App

Java Program to Print Pyramid Number Pattern

Last Updated : 06 Sep, 2022
Comments
Improve
Suggest changes
5 Likes
Like
Report

Here we are getting a step ahead in printing patterns as in generic we usually play with columnar printing in the specific row keep around where elements in a row are either added up throughout or getting reduced but as we move forward we do start playing with rows which hold for outer loop in our program.

Illustrations:

A pyramid number pattern of row size r = 5 would look like:
        1 
      2 3 2 
    3 4 5 4 3 
  4 5 6 7 6 5 4 
5 6 7 8 9 8 7 6 5
A pyramid number pattern of row size r = 4 would look like:
      1 
    2 3 2 
  3 4 5 4 3 
4 5 6 7 6 5 4 

Approach:

  1. For loop will be used to print each row in the pyramid.
  2. Inside the for loop we will use two loops :
  3. One loop is used to print the spaces
  4. The second loop will be used to print the numbers.

Implementation:


Output
            1  
         2  3  2  
      3  4  5  4  3  
   4  5  6  7  6  5  4  
5  6  7  8  9  8  7  6  5  

Time complexity: O(N^2) where N is given row size.
Auxiliary space: O(1), using constant extra space.


Next Article
Article Tags :
Practice Tags :

Similar Reads