ICTAK04 - Editorial

Problem Link:

Difficulty:
Easy-Medium

Prerequisites:
Loops

Problem:
Create a reverse pyramid of numbers for a given integer number.
Explanation:

In Reverse pyramid number pattern of N rows, i th row contains (i-1) space characters followed by (2*i - 1) characters. First take the number of rows in the pattern as input from user and store it in an integer variable “rowCount”. One iteration of outer for loop will print a row of inverted pyramid. For any row i, inner for loop
• Step:1 first prints i-1 spaces followed by a for loop which prints “rowCount” number of character.( i.e. numbers from 1 to rowCount)
For an example if rowCount=5, after completing this step of inner loop the output will be “1 2 3 4 5”
• Step:2 then the next for loop will print “rowCount-1” number of characters( i.e. numbers from rowCount-1 to 1)
For an example if rowCount=5, after completing first step of inner loop the output will be “1 2 3 4 5” and at the end this step of inner loop, output will be “1 2 3 4 5 4 3 2 1”

Eg : rowCount= 5
• After first outer loop iteration:
1 2 3 4 5 4 3 2 1
• After second outer loop iteration:
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
• After third outer loop iteration:
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
• After fourth outer loop iteration:
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
• After fifth outer loop iteration:
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
Solution:

import java.util.;
import java.lang.
;
import java.io.*;
class MainClass
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int noOfRows = sc.nextInt();
int rowCount = noOfRows;

   for (int i = 0; i < noOfRows; i++)
    {
                   for (int j = 1; j <= i*2; j++)
        {
            System.out.print(" ");
        }
         for (int j = 1; j <= rowCount; j++)             
        {
            System.out.print(j+" ");
        }
         for (int j = rowCount-1; j >= 1; j--)
        {                 
            System.out.print(j+" ");             
        }
        System.out.println();
         rowCount--;
    }
}

}