Well, I need to code a pyramid that prints the following pyramid WITHOUT the stars


In the center, there are prime numbers, from 1 to 9, and after that, they cycle. (1,3,5,7,9,1,3,5,7,9 and so on).

I made the code for the pyramid and it works.

#include <stdio.h>

int main(){
        int rows,star,spaces,num_row,num_star;

        printf("Insert number of stars\n");
        num_row = num_star;
        for (rows=1; rows <= num_row; rows++)
                for (spaces=1; spaces <= num_star; spaces++)
                        printf(" ");
                for (star=1; star <= rows; star++)
                        printf(" ");
        return 0;

But I don't know how to implement the necessary code to print the number pyramid. The point is, it can only have a maximum of 10 lines.

Can anybody help me? I'm lost here :(

4 Years
Discussion Span
Last Post by deceptikon

the row number should be odd so the condition in the inner loop that prints the number should have a limit of row*2-1

for the numbers use conditions that prints out the current row and by incrementing a counter until the odd number corresponding to that row is found then decrement the counter


But I don't know how to implement the necessary code to print the number pyramid.

It's a little more complex than what you've done. I'd suggest breaking each line down into four parts:

  1. Leading whitespace.
  2. The leading half of the row.
  3. The midline of the row.
  4. The trailing half of the row.

Leading whitespace is obvious. For the numbers, let's say you have the line "34543". The leading half is 34, then 5 is the midline, then 43 is the mirrored leading half that acts as the trailing half. You'd count up to get the leading half, then count down to get the trailing half.

The rotating numbers would be tricky if you used straight arithmetic like most of the number pyramid exercises, so I'd recommend storing the numbers separately and using a straight width counter to determine how many numbers to print. When you reach 10 counting up, reset the number to 0, and when you reach -1 counting down, reset to 9.

An alternative is to use a string as the "number list" and a rolling index to handle the wrapping. The rolling index would work the same way as above. The rolling behavior can also be performed with the remainder operator and modulo arithmetic, but that might be confusing things too much. ;)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.