Professional Documents
Culture Documents
6.1 Code Star Pattern Hint
6.1 Code Star Pattern Hint
Problem Description: You are given with an input number N, then you have to print the given
star pattern corresponding to that number N.
For example if N=4
Pattern output : *
***
*****
*******
How to approach?
1. Take N as input from the user.
2. Figure out the number of rows, (which is N here) and run a loop for that.
3. Now, figure out the number of columns in ith row ( i.e. 2*(i)-1)and run a loop for that
within this. Here, first you need to run a loop to print the spaces too.
4. Now, figure out “What to print?” in a particular row, column number. Here we have to
print “*”.
● i=2(<=4)
➔ 4-2=2 spaces are getting printed first.
➔ j=1 (<=2*2-1), so print “*”
➔ j=2 (<=2*2-1), so print “*”
➔ j=3 (<=2*2-1), so print “*”
➔ j=4(>2*2-1), move out of the inner loop with a new line
● i=3(<=4)
➔ 4-3=1 space is getting printed first.
➔ j=1(<=2*3-1), so print “*”
➔ j=2(<=2*3-1), so print “*”
➔ j=3(<=2*3-1), so print “*”
➔ j=4(<=2*3-1), so print “*”
➔ j=5(<=2*3-1), so print “*”
➔ j=6(>2*3-1), move out of the inner loop with a new line
● i=4(<=4)
➔ 4-4=0 no space is getting printed.
➔ j=1(<=2*4-1), so print “*”
➔ j=2(<=2*4-1), so print “*”
➔ j=3(<=2*4-1), so print “*”
➔ j=4(<=2*4-1), so print “*”
➔ j=5(<=2*4-1), so print “*”
➔ j=6(<=2*4-1), so print “*”
➔ j=7(<=2*4-1), so print “*”
➔ j=8(>2*4-1), move out of the inner loop with a new line
So , final output:
*
***
*****
*******