Print Diamond Pattern in C

To print a diamond pattern in C language, you can use nested loops. Here is a simple example code:

#include <stdio.h>

int main() {
    int rows, i, j, space;

    printf("Enter number of rows: ");
    scanf("%d", &rows);

    for(i = 1; i <= rows; i++) {
        for(space = 1; space <= rows - i; space++) {
            printf(" ");
        }
        for(j = 1; j <= 2 * i - 1; j++) {
            printf("*");
        }
        printf("\n");
    }

    for(i = rows - 1; i >= 1; i--) {
        for(space = 1; space <= rows - i; space++) {
            printf(" ");
        }
        for(j = 1; j <= 2 * i - 1; j++) {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

In this code, it starts by determining the number of rows in the diamond shape based on user input. Then it prints the top half of the diamond using nested loops, followed by another set of nested loops to print the bottom half. Finally, the diamond pattern is displayed. You can adjust the number of rows or the shape of the pattern as needed.

bannerAds