C program to print name inside heart pattern using for loop.

In C programming, creating a heart pattern with a name in the center is an interesting application of nested for loops. This program generates a heart shape using asterisks (*) and displays a user-entered name in the middle of the pattern.

Syntax

// Basic heart pattern structure
for(i = start; i <= end; i++) {
    // Upper heart curves
}
for(i = start; i >= end; i--) {
    // Lower triangular part
}

Algorithm

Follow these steps to create a heart pattern with name −

  • Step 1: Declare variables for loop counters, name array, and pattern size
  • Step 2: Read the name and number of rows from user
  • Step 3: Calculate the length of the name using strlen()
  • Step 4: Print the upper curved part of the heart using nested loops
  • Step 5: Print the lower triangular part with the name embedded in center

Example

Here is the complete C program to print a heart pattern with name using for loops −

Note: This example uses fgets() instead of the deprecated gets() function for safer string input.
#include <stdio.h>
#include <string.h>

int main() {
    int i, j, n;
    char name[50];
    int len;
    
    printf("Enter your name: ");
    fgets(name, sizeof(name), stdin);
    
    // Remove newline character from fgets
    name[strcspn(name, "<br>")] = '\0';
    
    printf("Enter no of rows: ");
    scanf("%d", &n);
    
    len = strlen(name);
    
    // Print upper part of the heart shape with stars
    for(i = n/2; i <= n; i += 2) {
        for(j = 1; j < n-i; j += 2) {
            printf(" ");
        }
        for(j = 1; j <= i; j++) {
            printf("*");
        }
        for(j = 1; j <= n-i; j++) {
            printf(" ");
        }
        for(j = 1; j <= i; j++) {
            printf("*");
        }
        printf("<br>");
    }
    
    // Print lower triangular part with stars
    for(i = n; i >= 1; i--) {
        for(j = i; j < n; j++) {
            printf(" ");
        }
        
        // Print the name in the center row
        if(i == n) {
            for(j = 1; j <= (n * 2 - len)/2; j++) {
                printf("*");
            }
            printf("%s", name);
            for(j = 1; j < (n * 2 - len)/2; j++) {
                printf("*");
            }
        } else {
            for(j = 1; j <= (i * 2) - 1; j++) {
                printf("*");
            }
        }
        printf("<br>");
    }
    
    return 0;
}

Output

When the above program is executed, it produces the following output −

Enter your name: TutorialsPoint
Enter no of rows: 10
    ***** *****
  ******* *******
 ********* *********
  **TutorialsPoint*
  *****************
   ***************
    *************
     ***********
      *********
       *******
        *****
         ***
          *

How It Works

  • Upper Part: Creates two curved sections using nested loops with incremental spacing
  • Lower Part: Forms a triangular shape with decreasing width
  • Name Placement: Embeds the name in the widest row with surrounding stars
  • Spacing: Uses calculated spaces to center-align the entire pattern

Key Points

  • The pattern size depends on the number of rows entered by the user
  • The name is automatically centered in the middle row of the heart
  • Use fgets() for safe string input instead of deprecated gets()
  • The algorithm handles names of different lengths dynamically

Conclusion

This heart pattern program demonstrates effective use of nested for loops and string manipulation in C. It creates a visually appealing pattern while showcasing fundamental programming concepts like loops, conditionals, and string handling.

Updated on: 2026-03-15T14:10:26+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements