create heart patterns in C++

C++ Heart Pattern made by Control statement, Program will print the Heart pattern according to the user input meaning that heart size will be depend on input values, for good heart patter use number 4- 8 according Turbo monitor. If the user input is 0 then program will exit while input value is a odd type then first we make this value as even type for perfect design. Here is the condition we used,

if(n==0)  
       exit(0);  
  if(n%2!=0)   
       x=n+1;  
  else   
       x=n;

Note There is hundreds of way to make same pattern, the below is the one of them, Here is the Program,

#include<iostream.h>
#include<stdlib.h> //exit()
using namespace std;

int main()
 {

   int n,i=0,v,x;

    cout<<"Enter No-> ";
    cin>>n;

      if(n==0)
            exit(0);
      if(n%2!=0) 
            x=n+1; 
      else 
            x=n;

      if(n<=5) v = (x-x/2)-1;
      if(n>5) v = (x-x/2);

   while(i<n)
     {
       for(int r=n-i;r>0;r--)
             cout<<" ";
       for(int j=0; j<=i*2;j++) 
         {
            if(i<=v)
              {
                if(i==i)
                cout<<" "; 
              }
           else
                cout<<"*"; 
         }
       for(int l=n*2; l>j;l--)
            cout<<" "; 

       for(j=0; j<=i*2;j++)
         {
           if(i<=v)
             {
               if(i==i)
                   cout<<" ";
             }
           else
               cout<<"*"; 
         }
       cout<<endl;
       i++;
    }
 
  int j=0;
  while(j<n-2)
    {
      for(int p=0; p<=n*4;p++)
            cout<<"*";

      cout<<endl;
      j++;
    }

  int k=0,s=0;
  while(k<n*2-2)
    {
      for(int r=0;r<k*2;r++)
           cout<<" ";
   
      for(int p=n*4; p>=s;p--)
           if(k==0&&p==n*4||p==0)
                    cout<<" ";
           else
                    cout<<"*";
          s++; s++;s++;s++;
          
      cout<<endl;
      k++;
    }
 return 0;
 }

OUTPUT

Enter No-> 7                
                                        
  *********   *********
 *********** ***********
*************************
*************************
*************************
*************************
 *********************** 
  *********************
    *****************
      *************
        *********
          *****
            *

more about patterns,


C++ Examples

Like it?

Leave a Reply

Your email address will not be published. Required fields are marked *