Last Updated: 27 Nov, 2020

Mirror image of triangle

Easy
Asked in companies
Hewlett Packard EnterpriseAirtel

Problem statement

Ninja’s younger sister got a project to print the pattern for the given number of rows, say ‘N’. She finds it difficult to write such a pattern for every ‘N’.

Ninja decided to help his sister and decided to write a code for this but wasn't able to write the code for the same. Ninja wants help to write a code and asks you for help. Help Ninja.

Example:

Pattern for N = 2
    0
   101
  21012
Input format :
The first line of input contains an integer ‘T’ denoting the number of test cases. And each test case follows:

Each test case contains an integer ‘N’ representing the number of rows.
Output format :
Each test case print 'N' strings denoting the required pattern of integers for a given integer ‘N’.

The output of each test case will be printed on a separate line
Constraints :
1 <= T <= 5
1 <= N <= 100

Time Limit: 1 sec.

Approaches

01 Approach

The idea is to use nested iterations for generating spaces and the digits inside the pattern.

 

Approach :

 

  • First, initialize two integer variables, say ‘number1’ and ‘number2’ to print the digits in the pattern and initialize both the variables with value = 1;
  • Make an iteration with iterator pointer ‘i’ to print the number of rows which will iterate from 0 to ‘N’.
    • Make a nested iteration to generate the spaces in the pattern with an iterator pointer ‘j’ which will iterate from ‘N’ - 1 to ‘i’ and print the spaces.
    • Make an iteration to print the digits inside the pattern with an iterator pointer ’k’ from 1 to ‘number1’and print the value = (‘k’ - ‘number2’).
    • Increment ‘number1’ with value 2.
    • Increment ‘number2’ with value 1.
    • Leave a line after that.