C Online Compiler
Example: Hollow Star Diamond in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Hollow Star Diamond #include <stdio.h> int main() { int n, i, j; // Step 1: Get user input for the number of rows (center width) printf("Enter the number of rows for the hollow diamond (odd number recommended): "); scanf("%d", &n); // Step 2: Print the upper half of the hollow diamond for (i = 1; i <= n; i++) { // Print leading spaces for (j = 1; j <= n - i; j++) { printf(" "); } // Print stars for the current row for (j = 1; j <= 2 * i - 1; j++) { // Print star only at the first position, last position, or if it's the first row if (j == 1 || j == 2 * i - 1) { printf("*"); } else { printf(" "); } } printf("\n"); } // Step 3: Print the lower half of the hollow diamond for (i = n - 1; i >= 1; i--) { // Print leading spaces for (j = 1; j <= n - i; j++) { printf(" "); } // Print stars for the current row for (j = 1; j <= 2 * i - 1; j++) { // Print star only at the first position or last position if (j == 1 || j == 2 * i - 1) { printf("*"); } else { printf(" "); } } printf("\n"); } return 0; }
Output
Clear
ADVERTISEMENTS