YouTip LogoYouTip

C Exercise Example23

# C Programming Exercise - Print a Diamond Pattern In this tutorial, you will learn how to print a symmetric diamond pattern of stars (`*`) using nested loops in C. This is a classic programming exercise designed to help developers master loop control structures, nested loops, and mathematical relationships in console output. --- ## Problem Description Write a C program to print the following diamond pattern to the console: ```text * *** ***** ******* ***** *** * ``` --- ## Algorithm Analysis To solve this problem efficiently, we can divide the diamond into two distinct parts: 1. **The Upper Half (including the middle line):** This consists of the first 4 rows. 2. **The Lower Half:** This consists of the remaining 3 rows. We will use nested `for` loops to control the output: * **Outer Loop:** Controls the current row being printed. * **Inner Loop 1:** Prints the leading spaces for alignment. * **Inner Loop 2:** Prints the asterisks (`*`). ### Mathematical Relationship #### 1. Upper Half (Rows 0 to 3) Let $i$ be the row index (from $0$ to $3$): * **Spaces:** The number of leading spaces decreases as the row index increases. The formula for spaces in row $i$ is $3 - i$. * **Asterisks:** The number of asterisks increases as the row index increases. The formula for asterisks in row $i$ is $2i + 1$. #### 2. Lower Half (Rows 0 to 2) Let $i$ be the row index (from $0$ to $2$): * **Spaces:** The number of leading spaces increases as the row index increases. The formula for spaces in row $i$ is $i + 1$. * **Asterisks:** The number of asterisks decreases as the row index increases. The formula for asterisks in row $i$ is $5 - 2i$. --- ## C Source Code Implementation Below is the complete, ready-to-run C program to print the diamond pattern: ```c #include int main() { int i, j, k; // 1. Print the upper half of the diamond (Rows 0 to 3) for (i = 0; i <= 3; i++) { // Print leading spaces for (j = 0; j <= 2 - i; j++) { printf(" "); } // Print asterisks for (k = 0; k <= 2 * i; k++) { printf("*"); } // Move to the next line printf("\n"); } // 2. Print the lower half of the diamond (Rows 0 to 2) for (i = 0; i <= 2; i++) { // Print leading spaces for (j = 0; j <= i; j++) { printf(" "); } // Print asterisks for (k = 0; k <= 4 - 2 * i; k++) { printf("*"); } // Move to the next line printf("\n"); } return 0; } ``` --- ## Output When you compile and run the program, it will produce the following output: ```text * *** ***** ******* ***** *** * ``` --- ## Key Considerations & Best Practices 1. **Loop Boundaries:** Pay close attention to the loop conditions (e.g., `j <= 2 - i` and `k <= 2 * i`). Using `<` versus `<=` changes the number of iterations, which can distort the shape of the diamond. 2. **Code Scalability:** The current solution uses hardcoded limits for a 7-row diamond. To make this program dynamic, you can prompt the user to input the number of rows and calculate the space/asterisk limits dynamically using variables instead of hardcoded constants. 3. **Formatting:** Ensure that there are no trailing spaces after the asterisks on each line to keep the console output clean and efficient.
← C Exercise Example24C Exercise Example21 β†’