Introduction
Printing a heart shape pattern using stars (*
) is a fun programming exercise that helps in understanding the use of loops, conditional logic, and formatting output. The heart shape requires precise control over the number of spaces and stars to create the right visual effect.
Problem Statement
Create a C program that:
- Prints a heart-shaped pattern using stars (
*
). - Uses nested loops and conditional logic to control the placement of stars and spaces.
Example:
*** ***
***** *****
******* *******
*************
***********
*********
*******
*****
***
*
Solution Steps
- Divide the Heart into Two Parts: The heart consists of an upper part with two semi-circles and a lower part that resembles a downward-pointing triangle.
- Use Nested Loops: The outer loops will control the rows, and the inner loops will control printing stars and spaces for both parts.
- Display the Heart Shape: Print stars and spaces in such a way that they form a heart shape.
C Program
#include <stdio.h>
int main() {
int i, j, size = 6;
// Step 1: Print the upper part of the heart (two semi-circles)
for (i = size / 2; i <= size; i += 2) {
// Print spaces before the first semi-circle
for (j = 1; j < size - i; j += 2) {
printf(" ");
}
// Print stars for the first semi-circle
for (j = 1; j <= i; j++) {
printf("*");
}
// Print spaces between the two semi-circles
for (j = 1; j <= size - i; j++) {
printf(" ");
}
// Print stars for the second semi-circle
for (j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
// Step 2: Print the lower part of the heart (downward triangle)
for (i = size; i >= 1; i--) {
// Print spaces before the stars
for (j = i; j < size; j++) {
printf(" ");
}
// Print stars to form the downward triangle
for (j = 1; j <= (i * 2) - 1; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Explanation
Step 1: Print the Upper Part of the Heart
- The upper part of the heart consists of two semi-circles. The outer loop controls the rows, and the inner loops handle printing spaces and stars.
- First, spaces are printed before the stars for the first semi-circle.
- After printing the stars for the first semi-circle, spaces are printed between the two semi-circles.
- Finally, the stars for the second semi-circle are printed.
Step 2: Print the Lower Part of the Heart
- The lower part of the heart is a downward-pointing triangle. The outer loop controls the number of rows, and the inner loops handle printing spaces and stars.
- First, spaces are printed before the stars to align them correctly.
- Stars are printed in decreasing order to form the downward triangle.
Output Example
For size = 6
, the output will be:
*** ***
***** *****
******* *******
***************
*************
***********
*********
*******
*****
***
*
Conclusion
This C program prints a heart shape pattern using stars (*
). The program uses nested loops to control the printing of stars and spaces in such a way that they form a heart shape. This exercise helps in practicing control structures, loops, and conditional statements in C programming.
Comments
Post a Comment
Leave Comment