C Exercises

Sep 2, 2020

C Program to Print Heart Shape Star Pattern

Ex: Write a C program to print heart shape star pattern. How to write a C program to print heart shape star pattern. C program  to print heart shape star pattern.


Input from user:

Enter the number: 5

Expected output:
    **     **
  ****   ****
   *********
     *******
       *****
         ***
           *




  Step by step logic of the given program:


1. Accept input number from user.

2. To print upper part of the pattern:
  • Print left peak:
     **
   ****

  • Print right peak:
     **
   ****

3. After that print lower part means inverted triangle:
*********
  *******
    *****
      ***
        *

4. Then you will get heart shape pattern:
    **     **
  ****  ****
  *********
    *******
      *****
        ***
          *




Program:

#include<stdio.h>
void main()
{
int no,i,j,k,z;
printf("Enter the number:\n");
scanf("%d",&no);
/*To print upper part*/
z=no*2;
for(i=no/2;i<=no;i=i+2)
{
/*To print left peak */
for(j=1;j<no-i;j=j+2)
{    
                    printf(" ");
}
for(k=1;k<=i;k++)
{
printf("*");
}
/*To print right peak*/
for(j=1;j<=no-i;j++)
{
    printf(" ");
}
for(k=1;k<=i;k++)
{
printf("*");
}
printf("\n");
}
/*To print lower part means inverted triangle*/
for(i=1;i<=no;i++)
{
for(j=1;j<i;j++)
{
printf(" ");
}
for(k=1;k<z;k++)
{
printf("*");
}
z-=2;
printf("\n");
}
}


Above program shows the following output:

C program to print heart shape star pattern, heart pattern in C



No comments:

Post a Comment

If you have any doubts, please discuss here...