Home / Programs / C program to print heart star pattern with name in center ***** ***** ******* ******* ********* ********* *******atnyla****** ***************** *************** ************* *********** ********* ******* ***** *** *
Programming Example

C program to print heart star pattern with name in center
  *****     *****
 *******   *******
********* *********
*******atnyla******
 *****************
  ***************
   *************
    ***********
     *********
      *******
       *****
        ***
         *

👁 2,012 Views
💻 Practical Program
📘 Step by Step Learning
Write a C program to print heart star pattern with name in the center. How to print heart star pattern with the name in center using for loop in C programming. Program to print the below heart star pattern.

Program Code

/**
 * C program to print heart star pattern with center name
 */
 
#include"stdio.h"
#include"string.h"
 
int main()
{
    int i, j, n;
    char name[50];
    int len;

    printf("Enter your name: ");
    gets(name);
 
    printf("Enter value of n : ");
    scanf("%d", &n);

    len = strlen(name);

    // Print upper part of the heart shape
    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("\n");
    }
 
    // Prints lower triangular part of the pattern
    for(i=n; i >= 1; i--)
    {
        for(j=i; j < n ; j++)
        {
            printf(" ");
        }
        
        // Print the name
        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("\n");
    }
 
    return 0;
}

Output

Enter your name: atnyla
Enter value of n : 10
  *****     *****
 *******   *******
********* *********
*******atnyla******
 *****************
  ***************
   *************
    ***********
     *********
      *******
       *****
        ***
         *
Press any key to continue . . .

Explanation

Required knowledge

Basic C programming, For loop, Nested loop

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.