Click here to Skip to main content
15,389,911 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
    1
   121
  12321
 1234321
123454321


What I have tried:

I have tried but not get the correct program
Posted
Updated 17-Jun-22 22:08pm
v2
Comments
CPallini 17-Jun-22 10:44am
   
Post your code here, we could help you to fix it.
Mike Hankey 17-Jun-22 10:49am
   
If you have a problem with the logic, ask your instructor.
If you have a problem with the code, post what code you have.
Patrice T 17-Jun-22 11:05am
   
Are you sure about the first line "1 121" ?
Show your work so far to get help.

I see a pattern, there!

At row r start counting with c=1 and increment it until c==r, then start decrementing it, until c==1 again. At each step output the value of c.
   
If you look at the numbers you can see what is the common factor.
Line 1 starts at 1 goes up to 2 and back down to 1.
Line 2 starts at 1 goes up to 3 and back down to 1.
... and so on until the limit gets to 5.

So you probably need three loops:
- The outer loop counts from 1 to 5
- Inner loop 1 counts up from 1 to the outer count.
- Inner loop 2 counts down from outer count to 1.

At each point in the inner loop you need to print the value of the loop counter.
   
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
   
You only need two loops.One for each line and one that writes the matching numbers to the right place.It is best to first calculate how long the longest line is(b), then at which position you want to write(k), and the number you want to write(x).Of course you have to convert the integer to a readable number(c).When the line is complete you can simply output it.
C
char line[18] = "                 ";
char c;
int h = ...;	// pyramid hight 
int b = ...;    // pyramid base len

for (int i=1; i<=h; i++) {
     for (int k=b/2, x=i; x>0; --k, --x) {
         char c = cnv2char(x);
         line[k] = c;      // left side
         line[b-k-1] = c;  // right side
     }
     printf("%s\n", line);
}
   

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