C Online Compiler
Example: Solid Star Diamond in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Solid 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 diamond (odd number recommended): "); scanf("%d", &n); // Step 2: Print the upper half of the diamond for (i = 1; i <= n; i++) { // Print leading spaces for (j = 1; j <= n - i; j++) { printf(" "); } // Print stars for (j = 1; j <= 2 * i - 1; j++) { printf("*"); } printf("\n"); } // Step 3: Print the lower half of the diamond for (i = n - 1; i >= 1; i--) { // Print leading spaces for (j = 1; j <= n - i; j++) { printf(" "); } // Print stars for (j = 1; j <= 2 * i - 1; j++) { printf("*"); } printf("\n"); } return 0; }
Output
Clear
ADVERTISEMENTS