C / C++ Program to Print A Heart On Screen, using Characters

Leave a Comment
/* The following program will ask about the size of heart you wanna print. Then it will display the heart shape. Try to keep the input value below 20 in order to get symmetric shape. The lower part is essentially a triangle. For the upper half it is just drawing two semi-circles of size radius. So, it chooses the center point for each semicircle, then computes the distance from the center to the current point (basic Pythagorean formula). If that distance is less than the chosen radius from the center of either circle, it prints an a. Otherwise it prints a space. And the lower half is just a triangle*/

//CODE:-
#include <stdio.h>
#include <math.h>
#include<stdlib.h>
int main()
{

    double x, y, size;
    printf("\nHow Big is Your Heart? (Enter <20):");
    scanf("%lf",&size);
    for (x=0; x<size; x++)
    {
        for (y=0; y<=4*size; y++)
        {
            double dist1 = sqrt( pow(x-size,2) + pow(y-size,2) );
            double dist2 = sqrt( pow(x-size,2) + pow(y-3*size,2) );
            if (dist1 < size + 0.5 || dist2 < size + 0.5 )
                 printf("%c",97);
//For 'a'
            else
                 printf(" ");
        }
        printf("\n");
    }
    for (x = 1; x <= 2*size; x++)
    {
        for (y=0; y<x; y++)
        printf(" ");
        for (y=0; y<4*size + 1 - 2*x; y++)
            printf("%c",86);
//for 'V'
        printf("\n");
    }
    system("pause");
    return 0; 
}


SCREENSHOT:
C / C++ Program to Print A Heart On Screen, using Characters: C C++ Heart
C / C++ Program to Print A Heart On Screen, using Characters: C C++ Heart

0 comments: