How to write this pyramid in c | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

14th Oct 2019, 1:11 PM
Pubas Deedumrong
Pubas Deedumrong - avatar
10 Answers
+ 3
Show us your attempts first
14th Oct 2019, 1:12 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 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.
14th Oct 2019, 1:45 PM
Shadow
Shadow - avatar
+ 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
14th Oct 2019, 2:09 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
Anyway, here is my solution, but I suggest you try to make yours before checking it: https://code.sololearn.com/c2Am8TF07yEH/?ref=app
14th Oct 2019, 2:19 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
THANK YOU!!
14th Oct 2019, 2:22 PM
Pubas Deedumrong
Pubas Deedumrong - avatar
14th Oct 2019, 1:17 PM
Pubas Deedumrong
Pubas Deedumrong - avatar
+ 1
If you understood the first code, here is a shorter version of it: https://code.sololearn.com/cR9gHLZddRu8/?ref=app
14th Oct 2019, 2:37 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
here is an improved version of Aymene's code using printf width specifier: https://code.sololearn.com/c6uwzw93b2Vh/#c
14th Oct 2019, 4:12 PM
MO ELomari
+ 1
https://code.sololearn.com/cd548vwS69q2/?ref=app Ruby, so clear, so readable, so small
15th Oct 2019, 12:04 PM
Acc. Deleted
+ 1
Thanks Good question
15th Oct 2019, 10:50 PM
DEEPAK KUMAWAT
DEEPAK KUMAWAT - avatar