C Program to Print Heart Star Pattern
heart pattern in C: The following code displays, prints heart star shape (💓 love) pattern using stars (*) in C language. It also prints the name inside (in center) the heart.
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.
How to print heart pattern in C?
- Start
- Accept the height or size of the heart according to which we are going to print or display the heart star pattern.
- Print two small triangles using for loop for the upper curve of heart, like the two given below
Two small triangles *** *** ***** *****
- Below these two triangles print the usual inverted triangle to complete heart star pattern.
The usual inverted triangle *********** ********* ******* ***** *** *
- The complete generated heart star pattern will look something like
Heart star pattern *** *** ***** ***** *********** ********* ******* ***** *** *
- End
C program to print heart shape (love symbol) 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:
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***
*************************
***********************
*********************
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*
*/