Saturday, August 29, 2026

Printing Butterfly Pattern in C | Advanced Shape Patterns

The butterfly star pattern is an advanced shape pattern in C programming that tests your control over bilateral symmetry, dual-wing rendering, and dynamic interior space calculations.

Introduction

Mastering the butterfly pattern requires dividing the shape into two symmetrical halves, each constructed by printing left stars, middle spaces, and right stars across each row. This tutorial breaks down the visual logic, provides a full C implementation, and analyzes its performance complexity.

  • Prerequisites: Comfort with nested for loops, standard input/output (printf, scanf), and integer arithmetic.

  • Expected Output:


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

Deconstructing the Pattern Logic

For a half-height of n, the total height of the butterfly structure is 2n - 1 rows. We split the rendering logic into two symmetrical phases:

Phase 1: Upper Wings (i = 1 to n)

  • Left Wing: Prints i asterisks.

  • Center Gap: Prints 2(n - i) spaces to form the shrinking inner gap.

  • Right Wing: Prints $i$ asterisks.

Phase 2: Lower Wings (i = n - 1 down to 1)

  • Left Wing: Prints i asterisks.

  • Center Gap: Prints 2(n - i) spaces to expand the inner gap back outward.

  • Right Wing: Prints i asterisks.

Row Index (i)PhaseLeft Asterisks (i)Center Spaces (2(n−i))Right Asterisks (i)
1Upper181
2Upper262
3Upper343
4Upper424
5 (Center)Upper505
4 (Row 6)Lower424
3 (Row 7)Lower343
2 (Row 8)Lower262
1 (Row 9)Lower181

Code Implementation

#include <stdio.h>

int main() {
    int n, i, j;

    printf("Enter the number of rows for upper half: ");
    if (scanf("%d", &n) != 1 || n <= 0) {
        printf("Invalid input. Please enter a positive integer.\n");
        return 1;
    }

    // Phase 1: Upper Wings (Rows 1 to n)
    for (i = 1; i <= n; i++) {
        // Left Wing Asterisks
        for (j = 1; j <= i; j++) {
            printf("*");
        }
        // Center Gap Spaces
        for (j = 1; j <= 2 * (n - i); j++) {
            printf(" ");
        }
        // Right Wing Asterisks
        for (j = 1; j <= i; j++) {
            printf("*");
        }
        printf("\n");
    }

    // Phase 2: Lower Wings (Rows n - 1 down to 1)
    for (i = n - 1; i >= 1; i--) {
        // Left Wing Asterisks
        for (j = 1; j <= i; j++) {
            printf("*");
        }
        // Center Gap Spaces
        for (j = 1; j <= 2 * (n - i); j++) {
            printf(" ");
        }
        // Right Wing Asterisks
        for (j = 1; j <= i; j++) {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

Code Breakdown

  • Triple Inner Loops: Each row executes three consecutive inner loops: one for the left wing, one for the middle gap, and one for the right wing.

  • Decrementing Phase: Reversing the outer loop bounds in Phase 2 (for (i = n - 1; i >= 1; i--)) reuses the exact same inner wing and space formulas while guaranteeing perfect vertical reflection.

  • Solid Center Line: At i = n, the space formula 2(n - n) yields 0 spaces, creating a solid middle bar of 2n asterisks.

Compiling and Execution

Compile and execute using GCC:

Console Output:

Enter the number of rows for upper half: 5
*        *
**      **
***    ***
****  ****
**********
****  ****
***    ***
**      **
*        *

Common Mistakes & Troubleshooting

  • Duplicate Full Width Row: Starting Phase 2 at i = n instead of i = n - 1 prints two adjacent solid lines of length 2n, distorting the central axis.

  • Incorrect Space Multiplier: Forgetting the factor of 2 in 2 * (n - i) causes the wings to overlap prematurely.

  • Missing Line Breaks: Omitting printf("\n"); after the right wing loop forces the entire butterfly structure onto a single line.

Complexity Analysis

  • Time Complexity: O(n^2) because two sequential nested loop blocks execute over 2n - 1 rows with inner loops proportional to n.

  • Space Complexity: O(1) auxiliary memory space, requiring only scalar integer variables.

Conclusion

Coordinating bilateral loop logic with decrementing phase bounds provides a foundation for building complex, multi-segmented ASCII graphics.


For all Pattern Programs list click here

…till the next post, bye-bye & take care

Friday, August 28, 2026

Printing Hourglass in C | Advanced Shape Patterns

The hourglass star pattern is an iconic multi-phase geometry problem in C programming that tests your ability to mirror logic by connecting an inverted full pyramid seamlessly to a standard full pyramid.

Introduction

Mastering the hourglass pattern refines your mastery over multi-loop coordination, dynamic bounds calculation, and vertical symmetry. This tutorial breaks down the architectural logic, line-by-line algorithm, complete C source code, and performance complexity required to output a flexible hourglass pattern based on user input.

  • Prerequisites: Proficiency in nested for loops, standard input/output (printf, scanf), and integer arithmetic.

  • Expected Output:

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

Deconstructing the Pattern Logic

For a half-height of n, the total height of the hourglass is 2n - 1 rows. We split the structure into two sequential rendering phases:

Phase 1: Upper Inverted Pyramid (i = 1 to n)

  • Leading Spaces: Increases each row from 0 to n - 1 (i - 1 spaces).

  • Asterisks: Decreases in odd increments given by 2(n - i) + 1.

Phase 2: Lower Full Pyramid (i = 2 to n)

  • Leading Spaces: Decreases each row from n - 2 down to 0 (n - i spaces).

  • Asterisks: Increases in odd increments given by 2i - 1.

Row IndexPhaseLeading SpacesAsterisks
1Upper09
2Upper17
3Upper25
4Upper33
5 (Center)Upper41
2 (Row 6)Lower33
3 (Row 7)Lower25
4 (Row 8)Lower17
5 (Row 9)Lower09

Code Implementation

#include <stdio.h>

int main() {
    int n, i, j, space;

    printf("Enter the number of rows for upper half: ");
    if (scanf("%d", &n) != 1 || n <= 0) {
        printf("Invalid input. Please enter a positive integer.\n");
        return 1;
    }

    // Phase 1: Upper Inverted Pyramid (Rows 1 to n)
    for (i = 1; i <= n; i++) {
        // Print leading spaces
        for (space = 1; space < i; space++) {
            printf(" ");
        }
        // Print asterisks
        for (j = 1; j <= (2 * (n - i) + 1); j++) {
            printf("*");
        }
        printf("\n");
    }

    // Phase 2: Lower Full Pyramid (Rows 2 to n)
    for (i = 2; i <= n; i++) {
        // Print leading spaces
        for (space = 1; space <= n - i; space++) {
            printf(" ");
        }
        // Print asterisks
        for (j = 1; j <= (2 * i - 1); j++) {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

Code Breakdown

  • Upper Loop Phase: Iterates through rows 1 to n, reducing asterisks from 2n - 1 down to 1 to form the contracting top half.

  • Lower Loop Phase: Starts at i = 2 to prevent duplicating the single central asterisk vertex, expanding back out to 2n - 1 asterisks.

  • Line Break Sequencing: printf("\n"); executes after each inner character loop completes, ensuring precise row alignment.

Compiling and Execution

Compile and run the program using GCC:

Console Output:

Enter the number of rows for upper half: 5
*********
 *******
  *****
   ***
    *
   ***
  *****
 *******
*********

Common Mistakes & Troubleshooting

  • Duplicate Single Star Vertex: Starting Phase 2 at i = 1 prints two consecutive rows with a single asterisk (*), distorting the central focal point.

  • Off-by-One Space Shift: Using space <= i instead of space < i in Phase 1 adds an unnecessary extra leading space on the first row.

  • Incorrect Odd Count Formulas: Mixing up upper (2(n - i) + 1) and lower (2i - 1) star formulas breaks the symmetry.

Complexity Analysis

  • Time Complexity: O(n^2) because two sequential nested loop blocks execute outer iterations up to $n$ times with inner loops proportional to n.

  • Space Complexity: O(1) auxiliary memory space, using only scalar integer counters.

Conclusion

Symmetrically coupling inverted and upright nested loops enables you to construct complex hourglass geometries cleanly.



For all Pattern Programs list click here

…till the next post, bye-bye & take care