🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment