Hollow Diamond Pattern In C

Hollow Diamond Pattern In C


Embarking on the coding odyssey? Brace yourself for the captivating universe of programming with the “Hollow Diamond Pattern in C.” As an aspiring coder, envision wielding the magic of logic to craft a visual symphony on your screen.

This journey delves into the artistry behind code—transforming the ordinary into an extraordinary hollow diamond spectacle. Discover the synergy of loops and symbols, empowering you to paint with pixels in the digital realm. Unleash your creativity, as this pattern becomes your canvas, where every character tells a story. Embrace the challenge and thrill of coding, where the Hollow Diamond Pattern in C is your inaugural masterpiece.

How To Print Hollow Diamond Pattern In C

#include <stdio.h>

int main() {
    int i, j;

    // Upper part of the pattern
    for (i = 1; i <= 5; i++) {
        // Print spaces for the left side
        for (j = 1; j <= 5 - i; j++) {
            printf(" ");
        }

        // Print the first asterisk
        printf("*");

        // Print spaces for the middle
        for (j = 1; j < 2 * i - 1; j++) {
            printf(" ");
        }

        // Print the second asterisk (except for the first row)
        if (i > 1) {
            printf("*");
        }

        // Move to the next line
        printf("\n");
    }

    // Lower part of the pattern
    for (i = 4; i >= 1; i--) {
        // Print spaces for the left side
        for (j = 1; j <= 5 - i; j++) {
            printf(" ");
        }

        // Print the first asterisk
        printf("*");

        // Print spaces for the middle
        for (j = 1; j < 2 * i - 1; j++) {
            printf(" ");
        }

        // Print the second asterisk
        if (i > 1) {
            printf("*");
        }

        // Move to the next line
        printf("\n");
    }

    return 0;
}

Output

    *
   * *
  *   *
 *     *
*       *
 *     *
  *   *
   * *
    *

Explanation

  1. Upper part of the hollow diamond pattern: The first loop (for (i = 1; i <= 5; i++)) controls the rows for the top part of the hollow diamond.
    • a. for (j = 1; j <= 5 - i; j++): Prints spaces on the left side, adjusting for the row number.
    • b. printf("*"): Prints the first asterisk.
    • c. for (j = 1; j < 2 * i - 1; j++): Prints spaces in the middle, creating the hollow diamond shape.
    • d. if (i > 1) printf("*"): Prints the second asterisk, excluding the first row.
    • e. printf("\n"): Moves to the next line after completing each row.
  2. Lower part of the hollow diamond pattern: The second loop (for (i = 4; i >= 1; i--)) controls the rows for the bottom part of the hollow diamond.
    • a. Similar logic as the upper part, printing spaces on the left, the first asterisk, spaces in the middle, and the second asterisk.
  3. The program combines the upper and lower parts to create the hollow diamond pattern.
  4. return 0;: Indicates successful completion of the program to the operating system.

Conclusion

In conclusion, the C program presented above elegantly crafts a visually striking hollow diamond pattern using a combination of nested loops and strategic printing of asterisks and spaces. This coding adventure serves as a valuable lesson for aspiring programmers, showcasing the seamless integration of logic and creativity. By understanding the intricacies of loop control and character placement, one can transform a seemingly complex design into an achievable masterpiece. This hollow diamond pattern not only demonstrates the power of programming but also encourages budding coders to explore the vast landscape of possibilities within the world of coding.

Leave a Reply

Your email address will not be published. Required fields are marked *