C Program to Print a Heart Shape Pattern using Stars

1. Introduction

Printing patterns with characters is a classic exercise in C programming that helps beginners understand nested loops and conditionals better. In this tutorial, we're going to create a program that prints a heart shape using asterisks (*). This pattern is a bit more complex than the typical patterns, as it requires careful manipulation of the loops and conditions to achieve the desired shape.

2. Program Steps

1. Define the overall structure of the heart shape using loops.

2. Use conditional statements within these loops to print stars (*) and spaces at appropriate positions.

3. Combine these loops and conditions to form the top and bottom parts of the heart shape.

3. Code Program

#include <stdio.h>

int main() {
    int i, j;
    int n = 6; // Height of the heart

    // Top part of the heart
    for(i = n/2; i <= n; i += 2) {
        // Print left margin spaces
        for(j = 1; j < n-i; j += 2) {
            printf(" ");
        }

        // Print the first set of stars
        for(j = 1; j <= i; j++) {
            printf("*");
        }

        // Print the middle space
        for(j = 1; j <= n-i; j++) {
            printf(" ");
        }

        // Print the second set of stars
        for(j = 1; j <= i; j++) {
            printf("*");
        }

        printf("\n");
    }

    // Bottom part of the heart
    for(i = n; i >= 1; i--) {
        // Print left margin spaces
        for(j = i; j < n; j++) {
            printf(" ");
        }

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

        printf("\n");
    }

    return 0;
}

Output:

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

Explanation:

1. The program includes the stdio.h header for input and output operations.

2. The main function begins execution of the program. It declares integer variables i and j for loop control, and n for the height of the heart shape.

3. The top part of the heart is created using a for loop that increments by 2 to create the curved top part. It prints spaces for the left margin, then stars for the left bump, spaces in the middle, and stars for the right bump.

4. The loop condition and increment steps are carefully chosen to ensure that the top part of the heart expands outwards and then contracts to meet at the center.

5. The bottom part of the heart is printed using another for loop that decreases in size for each row, creating a narrowing effect towards the bottom of the heart.

6. Inside this loop, spaces are printed first to align the stars correctly, followed by stars that decrease in number each row to form the point of the heart.

7. The program uses nested loops and conditional logic to precisely control where spaces and stars are printed, creating the heart shape.

8. The program concludes with return 0;, indicating successful execution.


Comments