Diamond Star Pattern
In the Diamond Star Pattern it consist a two triangular patterns, one is facing upwards and second is the downward, with a common middle row. The stars are arranged in such a way that each row has a decreasing number of stars as it moves away from the center. The number of rows determines the height of the diamond, and the width is calculated based on the number of stars in the middle row.
Output
Enter the number of rows (odd number): 5
*
***
*****
***
*
Free E-books
Codes – Diamond Star Pattern
Here is the code of Diamond Star Pattern,
#include <stdio.h> int main() { int rows, i, j, space; printf("Enter the number of rows (odd number): "); scanf("%d", &rows); // Upper part of the diamond for (i = 1; i <= rows / 2 + 1; i++) { for (space = 1; space <= rows / 2 + 1 - i; space++) { printf(" "); } for (j = 1; j <= 2 * i - 1; j++) { printf("*"); } printf("\n"); } // Lower part of the diamond for (i = rows / 2; i >= 1; i--) { for (space = 1; space <= rows / 2 + 1 - i; space++) { printf(" "); } for (j = 1; j <= 2 * i - 1; j++) { printf("*"); } printf("\n"); } return 0; }
Code Explanation
- Firstly we show the prompts the user to enter the number of rows for the diamond (an odd number).
- Then it will divides the diamond into two parts: the upper part and the lower part.
- For each part, it iterates through each row using a
for
loop. - Within each row, it prints leading spaces to center-align the stars.
- Then it prints the stars using another nested
for
loop. - The number of stars in each row follows a pattern: increasing from 1 to the middle row, then decreasing back to 1.