🎓 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
A butterfly pattern is a symmetric star pattern that looks like the wings of a butterfly. It consists of two parts: an upper part where the stars form a wing-like structure, and a lower part which is the inverted version of the upper part. This exercise helps in understanding how to use loops and conditional logic to create symmetrical patterns.
Problem Statement
Create a C program that:
- Accepts the number of rows for the butterfly pattern.
- Prints a butterfly-shaped star pattern.
Example:
- Input:
rows = 5 - Output:
* * ** ** *** *** **** **** ********* **** **** *** *** ** ** * *
Solution Steps
- Input the Number of Rows: The size determines the number of rows for each half of the butterfly.
- Use Nested Loops: The outer loops handle the rows, and the inner loops handle printing the stars and spaces.
- Display the Butterfly Pattern: Print stars and spaces to form the butterfly structure.
C Program
#include <stdio.h>
int main() {
int i, j, rows;
// Step 1: Accept the number of rows for the butterfly pattern
printf("Enter the number of rows: ");
scanf("%d", &rows);
// Step 2: Print the upper part of the butterfly
for (i = 1; i <= rows; i++) {
// Print the left wing stars
for (j = 1; j <= i; j++) {
printf("*");
}
// Print spaces between the wings
for (j = 1; j <= 2 * (rows - i); j++) {
printf(" ");
}
// Print the right wing stars
for (j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
// Step 3: Print the lower part of the butterfly
for (i = rows; i >= 1; i--) {
// Print the left wing stars
for (j = 1; j <= i; j++) {
printf("*");
}
// Print spaces between the wings
for (j = 1; j <= 2 * (rows - i); j++) {
printf(" ");
}
// Print the right wing stars
for (j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Explanation
Step 1: Input Number of Rows
- The program starts by taking the input from the user to define the number of rows for each half of the butterfly pattern.
Step 2: Print the Upper Part of the Butterfly
- The outer loop controls the number of rows for the upper part.
- The first inner loop prints stars for the left wing.
- The second inner loop prints spaces between the left and right wings.
- The third inner loop prints stars for the right wing.
Step 3: Print the Lower Part of the Butterfly
- The second outer loop controls the rows for the lower part, which is the inverted version of the upper part.
- The inner loops print stars and spaces in reverse order to mirror the upper part.
Output Example
For rows = 5, the output will be:
* *
** **
*** ***
**** ****
**********
**** ****
*** ***
** **
* *
For rows = 6, the output will be:
* *
** **
*** ***
**** ****
***** *****
************
***** *****
**** ****
*** ***
** **
* *
Conclusion
This C program prints a butterfly pattern using nested loops to create symmetry between the upper and lower parts of the pattern. This exercise helps in practicing loop control, conditional printing, and creating symmetrical star patterns 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