This valentine, gift your loved ones an heart with a message written within it to express what you feel about them. Don’t miss the moment to surprise them.
This article demonstrates the code to surprise them. Best wish to all the programmer loved ones < 3.
Below is the code.
C++
// C++ code to print happy valentine's // day. #include <bits/stdc++.h> using namespace std; int main() { // initializing variables and size double a, b, n = 10; // initializing String to print in heart string message = " HappY Valentines DaY <3 "; // Position from where from top // message box would be placed. int print_message = 4; // add space if message length is odd if (message.length() % 2 != 0) message += " "; // outer loop to adjust length of upper // part message is not handled in this part for (a = 0; a < n; a++) { // to print space and variable accordingly for (b = 0; b <= 4 * n; b++) { // computing distance to print variable double distance1 = sqrt(pow(a - n, 2) + pow(b - n, 2)); double distance2 = sqrt(pow(a - n, 2) + pow(b - 3 * n, 2)); if (distance1 < n + 0.5 || distance2 < n + 0.5) cout << "S"; else cout << " "; } // ending line after each iteration cout << "\n"; } // printing the message part // and lower part of heart. // outer loop handles // depth of the heart. for (a = 1; a < 2 * n; a++) { // for getting the lower curve of heart for (b = 0; b < a; b++) cout << " "; // inner loop // handles the message and spaces accordingly for (b = 0; b < 4 * n + 1 - 2 * a; b++) { // checks if the height is in range of message // space if (a >= print_message - 1 && a <= print_message + 1) { int point = b - (4 * n - 2 * a - message.size()) / 2; // prints message after leaving appropriate space if (point < message.size() && point >= 0) { if (a == print_message) cout << message[point]; else cout << " "; } else cout << "S"; } else cout << "S"; } cout << endl; } } |
Java
// Java code to print happy valentine's day. class GFG { public static void main(String[] args) { // initializing variables and size double a, b, n = 10; // initializing String to print in heart String message = " HappY Valentines DaY <3 "; // Position from where from top // message box would be placed. int print_message = 4; // add space if message length is odd if (message.length() % 2 != 0) message += " "; // outer loop to adjust length of upper // part message is not handled in this part for (a = 0; a < n; a++) { // to print space and variable accordingly for (b = 0; b <= 4 * n; b++) { // computing distance to print variable double distance1 = Math.sqrt(Math.pow(a - n, 2) + Math.pow(b - n, 2)); double distance2 = Math.sqrt(Math.pow(a - n, 2) + Math.pow(b - 3 * n, 2)); if (distance1 < n + 0.5 || distance2 < n + 0.5) System.out.print("S"); else System.out.print(" "); } // ending line after each iteration System.out.println(); } // printing the message part // and lower part of heart. // outer loop handles // depth of the heart. for (a = 1; a < 2 * n; a++) { // for getting the lower curve of heart for (b = 0; b < a; b++) System.out.print(" "); // inner loop handles the message // and spaces accordingly for (b = 0; b < 4 * n + 1 - 2 * a; b++) { // checks if the height is in range // of message space if (a >= print_message - 1 && a <= print_message + 1) { double point = b - (4 * n - 2 * a - message.length()) / 2; // prints message after leaving // appropriate space if (point < message.length() && point >= 0) { if (a == print_message) System.out.print (message.charAt((int)point)); else System.out.print(" "); } else System.out.print("S"); } else System.out.print("S"); } System.out.println(); } } } // This code is contributed by Anant Agarwal. |
Output:
SSSSSSS SSSSSSS
SSSSSSSSSSS SSSSSSSSSSS
SSSSSSSSSSSSS SSSSSSSSSSSSS
SSSSSSSSSSSSSSS SSSSSSSSSSSSSSS
SSSSSSSSSSSSSSSSS SSSSSSSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSS SSSSSSSSSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSS SSSSSSSSSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
SSSS SSSSS
SSS HappY Valentines DaY <3 SSSS
SS SSS
SSSSSSSSSSSSSSSSSSSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSS
SSSSSSSSSSSSSSSSS
SSSSSSSSSSSSSSS
SSSSSSSSSSSSS
SSSSSSSSSSS
SSSSSSSSS
SSSSSSS
SSSSS
SSS
Sources :
- https://www.geeksforgeeks.org/printing-heart-pattern-c/
- Answer by Torne on https://stackoverflow.com/questions/20075775 /print-heart-shape-with-words-inside
Note : Works well with value of N atleast 8. You can alter the character you need to use (S in the case above.) .
Recommended Posts:
- Learn Data Structures and Algorithms for your Dream Job with this online Course
- Create Directory or Folder with C/C++ Program
- C Program to Swap two Numbers
- C++ Program to concatenate two strings using Operator Overloading
- C++ Program to implement Symbol Table
- C++ Program for Bubble Sort
- C++ Program for Heap Sort
- C/C++ Program for n-th Fibonacci number
- C++ Program for ShellSort
- C++ Program for Longest Common Subsequence
- C/C++ Program for Longest Increasing Subsequence
- C++ Program for Longest Increasing Subsequence
- A Shell program To Find The GCD | Linux
- C++ Program for GCD of more than two (or array) numbers
- C++ Program for Common Divisors of Two Numbers
- C++ Program for Bitonic Sort
- C++ Program for Bitonic Sort
- C++ Program for Recursive Bubble Sort
- C++ Program for BogoSort or Permutation Sort
- C++ Program for Gnome Sort
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

