0

The program asks the user for the pyramid hight i.e: rows number, and prints out a pyramid.

for input of 5, the result should look like this:

    *
   ***
  *****
 *******
*********

the code I wrote is:

#include <stdio.h>

void main()

{
    int i, j, n = 0;

    printf("enter pyramid hight: \t");
    scanf("%d", &n);

    // FOR EACH ROW:
    for (i = 0; i < n; i++)
    {
        // print spaces till middle:
        for (j = 0; j < (n - 1 - i); j++)
        {
            printf(" ");
        }

        // print stars:
        for (j = 0; j < 2 * n + 1; j++)
            ;
        {


            printf("*");

            // go to new row
            printf("\n");
        }
    }
}

but the result is:

    *
   *
  *
 *
*

what could be going wrong exactly, I think the second loop may be the problem but I can't put my hand on the reason.

sarah
  • 135
  • 5

1 Answers1

0

You got a logic errors for the second for loop which does not do what you want:

    // print stars:
    for (j = 0; j < 2 * n + 1; j++)  //<-- error #1 - n should be i as it's specific for this row
        ;  //<-- error #2 - this termination is completely wrong, it means this `for` loop doing nothing
    {


        printf("*");

        // go to new row
        printf("\n");
    }

which should be:

    // print stars:
    for (j = 0; j < 2 * i + 1; j++)
    {
        printf("*");
    }
    // go to new row
    printf("\n");
artm
  • 17,291
  • 6
  • 38
  • 54