Print heart star pattern in C and Java
Write a C and Java program to print the heart star pattern.
Want to impress another geek friend on some special occasion? Here’s the code to print the heart shape:
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <stdio.h> #include <math.h> // C program to print the heart star pattern int main() { // set output size int size = 10; for (int x = 0; x < size; x++) { for (int 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("*"); } else { printf(" "); } } printf("\n"); } for (int x = 1; x < 2*size; x++) { for (int y = 0; y < x; y++) { printf(" "); } for (int y = 0; y < 4*size + 1 - 2*x; y++) { printf("*"); } printf("\n"); } return 0; } |
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
class Main { // Java program to print the heart star pattern public static void main(String[] args) { // set output size final int N = 10; for (int i = 0; i < N; i++) { for (int j = 0; j <= 4 * N; j++) { double d1 = Math.sqrt(Math.pow(i - N, 2) + Math.pow(j - N, 2)); double d2 = Math.sqrt(Math.pow(i - N, 2) + Math.pow(j - 3 * N, 2)); if (d1 < N + 0.5 || d2 < N + 0.5) { System.out.print('*'); } else { System.out.print(' '); } } System.out.print(System.lineSeparator()); } for (int i = 1; i < 2 * N; i++) { for (int j = 0; j < i; j++) { System.out.print(' '); } for (int j = 0; j < 4 * N + 1 - 2 * i; j++) { System.out.print('*'); } System.out.print(System.lineSeparator()); } } } |
Output:
We can also modify the above code to print the heart pattern with the desired text inside.
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
#include <stdio.h> #include <math.h> #include <string.h> // C program to print heart pattern int main() { int size = 10; char* message = " I love You "; int n = strlen(message); int print_line = 4; for (int x = 0; x < size; x++) { for (int 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("*"); } else { printf(" "); } } printf("\n"); } for (int x = 1; x < 2 * size; x++) { for (int y = 0; y < x; y++) { printf(" "); } for (int y = 0; y < 4 * size + 1 - 2 * x; y++) { if (x >= print_line - 1 && x <= print_line + 1) { int idx = y - (4 * size - 2 * x - n) / 2; if (idx < n && idx >= 0) { if (x == print_line) { printf("%c", message[idx]); } else { printf(" "); } } else { printf("*"); } } else { printf("*"); } } printf("\n"); } } |
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
class Main { // Java program to print heart pattern public static void main(String[] args) { final String message = " I love You "; final int n = 10; for (int i = 0; i < n; i++) { for (int j = 0; j <= 4 * n; j++) { double d1 = Math.sqrt(Math.pow(i - n, 2) + Math.pow(j - n, 2)); double d2 = Math.sqrt(Math.pow(i - n, 2) + Math.pow(j - 3 * n, 2)); if (d1 < n + 0.5 || d2 < n + 0.5) { System.out.print('*'); } else { System.out.print(' '); } } System.out.print(System.lineSeparator()); } for (int i = 1; i < 2 * n; i++) { for (int j = 0; j < i; j++) { System.out.print(' '); } for (int j = 0; j < 4 * n + 1 - 2 * i; j++) { if (i >= 2 && i <= 4) { int idx = j - (4 * n - 2 * i - message.length()) / 2; if (idx < message.length() && idx >= 0) { if (i == 3) { System.out.print(message.charAt(idx)); } else { System.out.print(' '); } } else { System.out.print('*'); } } else { System.out.print('*'); } } System.out.print(System.lineSeparator()); } } } |
Output:
That’s all about printing a heart star pattern in C and Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)