hi. i am marsh a student

guys i have a activity but i don't know to start it with a loop. can you help me?

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

Recommended Answers

All 4 Replies

use icode tags instead of quote tags to preserve the spacing.

hi. i am marsh a student

guys i have a activity but i don't know to start it with a loop. can you help me?

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

This is actually easier than the corresponding filled diamond because it's more obvious that you have to print the spaces to get a proper result. In this case, you can break the problem down to the smaller relevant part:

*****
****-
***--
**---
*----
**---
***--
****-
*****

I used dashes instead of spaces once again to make it clear that the whitespace is significant and needs to be printed. This problem is much easier, but once you do it, you can solve the whole problem because it's a simple matter of adding a single space and then doing the same thing in reverse to get the full result:

// Add a space for the center column
*****-
****--
***---
**----
*-----
**----
***---
****--
*****-
// Print the same line in reverse
*****-*****
****---****
***-----***
**-------**
*---------*
**-------**
***-----***
****---****
*****-*****

Done.

what is the code to display "*" as pyramid?

what is the code to display "*" as pyramid?

#include <stdio.h>

static void print_n(const char symbol, unsigned int n)
{
    while (n --> 0)
        printf("%c", symbol);
}

void print_pyramid(const unsigned int height)
{
    int       i           = 0,
              width       = 0;
    const int WIDTH_TOTAL = (2 * height) - 1;

    for (i = 1; i <= height; i++)
    {
        width = (2 * i) - 1;

        print_n(' ' , (WIDTH_TOTAL - width) / 2);
        print_n('*' , width);
        print_n('\n', 1);
    }
}

int main(void)
{
    print_pyramid(10);
    return 0;
}
commented: -1 for being an idiot -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.