Kodewick
Start learning →
Basic←→0 / 50

Print a diamond

#031Basicrow(n - i, 2 * i - 1)8 min

Print a diamond of *: the top half has n rows, and row i has 2i - 1 stars after n - i spaces; the bottom half is the same upside down, one row shorter. row(spaces, stars) is already written and prints one row. Finish diamond(n); the program prints one with n = 3 and one with n = 4.

Should show
  *
 ***
*****
 ***
  *
   *
  ***
 *****
*******
 *****
  ***
   *
main.c
#include <stdio.h>

void row(int spaces, int stars) {
    for (int i = 0; i < spaces; i++) printf(" ");
    for (int i = 0; i < stars; i++) printf("*");
    printf("\n");
}

void diamond(int n) {
    for (int i = 1; i <= n; i++) {
        // your code here: row i of the top half
    }
    for (int i = n - 1; i >= 1; i--) {
        // your code here: the bottom half, the same row as the top
    }
}

int main(void) {
    diamond(3);
    diamond(4);
    return 0;
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.