+ 1
How to write this pyramid in c
n=the last row of pyramid if n=7 if n=6 * ** *** **** ***** ****** ******* Now i got just this https://code.sololearn.com/cwf1L8KcI9qq/#c
10 Réponses
+ 3
Show us your attempts first
+ 3
Use a for loop for the rows, it should start either from one or two, depending on whether N is odd or not, with an increment of two, as each row two additional asterisks are printed, and run until the running variable is bigger than N.
Inside the outer for loop, you can use two for loops, one for the whitespaces, and one for the asterisks.
For a row index I, the first one prints ( N - I ) / 2 times a whitespace, e.g. in the first row, you need three whitespaces on the left, so ( 7 - 1 ) / 2 equals 3 whitespaces to be printed. The second loop simply prints I asterisks.
This is just one way to accomplish the pattern, there are multiple you could try out.
+ 2
You have many variables in your code, which makes it confusing, try to use less variables. Also, the loops you wrote have wrong conditions, give it another try using only one variable, and two loops in each condition.
The first one:
if(n%2==1){your two loops here}
Same for n%1==0
If you still can't find the solution, ask again
+ 2
Anyway, here is my solution, but I suggest you try to make yours before checking it:
https://code.sololearn.com/c2Am8TF07yEH/?ref=app
+ 2
THANK YOU!!
+ 1
+ 1
If you understood the first code, here is a shorter version of it:
https://code.sololearn.com/cR9gHLZddRu8/?ref=app
+ 1
here is an improved version of Aymene's code using printf width specifier:
https://code.sololearn.com/c6uwzw93b2Vh/#c
+ 1
https://code.sololearn.com/cd548vwS69q2/?ref=app
Ruby, so clear, so readable, so small
+ 1
Thanks
Good question