C Program to Print Heart Star Pattern

heart star pattern in C: The following code displays, prints heart star shape 💓 pattern using stars (*) in C language. It also prints the name inside (in center) the heart.

heart-dtar-pattern-prpgram-in-c

Algorithm to print heart shape star pattern in C

  1. Start
  2. Accept the height or size of the heart according to which we are going to print or display the heart star pattern.
  3. Print two small triangles using for loop for the upper curve of heart, like the two given below
    
    Two small triangles
    
     ***   ***
    ***** *****
    
    
  4. Below these two triangles print the usual inverted triangle to complete heart star pattern.
    The usual inverted triangle
    
    ***********
     *********
      *******
       *****
        ***
         *
    
  5. The complete generated heart star pattern will look something like
    
    Heart star pattern
    
     ***   ***
    ***** *****
    ***********
     *********
      *******
       *****
        ***
         *
    
  6. End

C program to print heart shape pattern

/* C program to print heart star pattern */

#include<stdio.h>

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

	printf("\n How many lines or size of heart:- ");
	scanf("%d",&size);

	printf("\n");
    
	for(i=size/2;i<=size;i+=2)
	{
		for(j=1;j<size-i;j+=2)
			printf(" ");

		for(j=1;j<=i;j++)
			printf("*");


		for(j=1;j<=size-i;j++)
			printf(" ");


		for(j=1;j<=i;j++)
			printf("*");

		printf("\n");
 }


	for(i=size;i>=1;i--)
	{
		for(j=i;j<size;j++)
			printf(" ");
 
		for(j=1;j<=(i*2)-1;j++)
			printf("*");

		printf("\n");
 }

 return 0;
}

/* Output of above code:-

 How many lines or size of heart:- 6

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

*/

Output of Program:

Output of C program to print heart shape pattern

We can also print heart shape pattern with any name in the center. Logic for C program to print heart star pattern with name in the center same is as above, just print the name in the lower triangle when the counter 'i' is equal to size of heart star pattern.

C programming code to print heart symbol with name

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

#include<stdio.h>
#include<string.h>

int main()
{
	int i,j,size,len;
	char mname[50];

	printf("Enter name or message to print in center of heart:- ");
	scanf("%[^\n]s",mname);

	printf("Enter size of heart:- ");
	scanf("%d",&size);

	len=strlen(mname);

	printf("\n");

	for(i=size/2;i<=size;i+=2)
	{
		for(j=1;j<size-i;j+=2)
			printf(" ");

		for(j=1;j<=i;j++)
			printf("*");

		for(j=1;j<=size-i;j++)
			printf(" ");

		for(j=1;j<=i;j++)
			printf("*");

		printf("\n");
	}

	for(i=size;i>=1;i--)
	{
		for(j=i;j<size;j++)
			printf(" ");

		if(i==size)
		{
			for(j=1;j<=(size*2-len)/2;j++)
				printf("*");

			printf("%s",mname);
 
			for(j=1;j<(size*2-len)/2;j++)
				printf("*");
		}
		else
		{
			for(j=1;j<=(i*2)-1;j++)
				printf("*");
		}

		printf("\n");
		}

	return 0;
}

/* Output of above code:-

Enter name or message to print in center of heart:- Computer Science AI
Enter size of heart:- 14

   *******       *******
  *********     *********
 ***********   ***********
************* *************
****Computer Science AI***
 *************************
  ***********************
   *********************
    *******************
     *****************
      ***************
       *************
        ***********
         *********
          *******
           *****
            ***
             *
*/