Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to change the output of this while loop code into a pyramid of asterisks. However, I am confused because I made a Christmas tree-like pyramid.

____*
___* *
__* * *
_* * * *
* * * * *

p.s. kindly treat underscores as spaces

What I have tried:

i3 = 0;
j3 = 0;
k3 = 0;
while i3<=n
  while k3<=(n-i3-1)
    fprintf(" ")
    k3 = k3 + 1;
  endwhile
  k3 = 0;
  while j3<(2*i3-1)
    fprintf("*")
    j3 = j3+1;
  endwhile
  j3 = 0;
  i3 = i3+1;
  fprintf("\n")
  endwhile
Posted
Updated 23-Jun-22 3:25am
v2
Comments
Richard MacCutchan 23-Jun-22 6:21am    
A Pascal Triangle consists of numbers, not asterisks.
CPallini 23-Jun-22 7:30am    
Richard is correct. Please define a 'Pascal pyramid of asterisks'.
pollie 2022 23-Jun-22 7:50am    
I'm sorry. This kind of pattern is what I meant. I edited now my question.
____*
___* *
__* * *
_* * * *
* * * * *

p.s. the underscores are not included, i just used it to adjust the pyramid

Do you mean this?
matlab
rows = 5;
r = 0;
while r < 5;
  x = -rows;
  while x <= rows
    if ( (x >= -r) && (x<= r) && mod(r,2) == mod(x,2))
      fprintf("*")
    else
      fprintf(" ")
    endif
    x = x + 1;
  endwhile
    fprintf("\n")
    r = r + 1;
endwhile
 
Share this answer
 
Comments
Patrice T 25-Jun-22 14:10pm    
+5
This is just a matter of counting, as you can see from your example picture. The final line is five stars, the previous line is one space and four stars, etc. So from the pattern it should be easy to calculate how many of each character you need to print on each line.
 
Share this answer
 
Quote:
How can I change this to a pyramid?

Look carefully at your pyramid:
____*
___*_*
__*_*_*
_*_*_*_*
*_*_*_*_*

There is more spaces than what you seems to think.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900