Last Updated: 25 Nov, 2020

Number Pattern 1

Easy
Asked in companies
AmazonBetsolAmdocs

Problem statement

You are given an integer 'N'. Your task is to print the following pattern for the given N number of rows.

For Example: Pattern for N = 4

1
11
111
1111
Input Format:
The first line contains a single integer ‘T’ representing the number of test cases. 

The first line of each test case will contain an integer ‘N’ that is the total number of rows.
Output Format:
For each test case, print the pattern of N lines.

Output for every test case will be printed in a separate line.
Constraints:
1 <= T <= 10
1 <= N <= 10^3

Time Limit: 1 sec

Approaches

01 Approach

The basic idea of this approach is to print the pattern row-wise. For each row, a pattern is observed: 1 is printed 1 time in the 1st row, 2 times in the 2nd row, 3 times in the 3rd row, and so on. So, a pattern is observed that the number of rows determines how many times 1 is printed in each row.


 Here is the algorithm:

 

  1. Run a loop for ‘ROW’ = 0 to ‘N’:
    • Run a loop for ‘COL’ = 0 to equal to ‘ROW’:
      • Print 1.
    • Print a new line.

02 Approach

The basic idea of this approach is to print the pattern row-wise. For each row, a pattern is observed: 1 is printed 1 time in the 1st row, 2 times in the 2nd row, 3 times in the 3rd row, and so on. So, a pattern is observed that the number of rows determines how many times 1 is printed in each row. So, we will initialize a string ‘STR’ = ‘1’ and then after every row, we will concatenate ‘1’ to the ‘STR’
 

Here is the algorithm:
 

  1. Run a loop for ‘ROW’ = 0 to ‘N’:
    • Print ‘STR’.
    • ‘STR’ += ‘1’
    • Print a new line.