Last Updated: 10 Oct, 2022

Star Triangle

Easy
Asked in company
Appinventiv

Problem statement

Ninja was very fond of patterns. For a given integer ‘N’, he wants to make the N-Star Triangle.

Example:
Input: ‘N’ = 3

Output: 

  *
 ***
*****
Input Format:
The first and only line of each test case contains an integer ‘N’.
Output format:
Return a 2D array of characters that represents the N-Star Pattern.
Constraints :
1  <= N <= 20
Time Limit: 1 sec

Approaches

01 Approach

Approach: 

The solution is iterating on the pattern and printing every cell with ‘*’ or spaces.

The steps are as follows :

Function void nStarTriangle(int ‘N’)

 

  1. Int ‘gap’ = ‘N’-1, ‘stars’ = 1.
  2. For ‘i’ from 0 to ‘N’-1:
    • For j from 0 to ‘gap’-1:
      • Print ’ ’
    • For j from gap+1 to gap+stars-1:
      • Print ’*’
    • gap–
    • star+=2