Last Updated: 25 Nov, 2020

Star Pattern

Easy
Asked in companies
PayPalInfo Edge India (Naukri.com)LTIMindtree

Problem statement

Print the following pattern

Pattern for N = 4

picture

The dots represent spaces.
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 <= 100
1 <= N <= 100

Approaches

01 Approach

The basic idea of this approach is to print the pattern row-wise. The pattern consists of ’N’ rows. Each row contains 2 * ‘N’ - 1 star. In addition to these stars, there are leading spaces. Each row contains ‘N’ - ‘Row’ spaces (where ‘ROW’ is the current row number).


 

Here is the algorithm:


 

  1. Run a loop for ‘ROW’ = 0 to ‘N’:
    • To print spaces, create a variable ‘SPACES’ and run a loop till ’N’ - ‘ROW.’
    • To print the stars ‘*’, run another loop from 1 to 2 * ‘ROW’ - 1:
      • Inside the loop print the star.
    • Print a new line.