🎓 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 cross star pattern is a symmetrical pattern where stars (*) are arranged in such a way that they form a cross. This exercise helps in understanding how to use nested loops and conditional statements to control the placement of stars and spaces.
Problem Statement
Create a C program that:
- Accepts the size of the pattern (which should be odd).
- Prints a cross-shaped star pattern.
Example:
- Input:
size = 5 - Output:
* * * * * * * * *
Solution Steps
- Input the Size: The size determines the height and width of the pattern. The size should be odd to create a symmetrical cross.
- Use Nested Loops: The outer loop controls the rows, and the inner loop controls the columns for printing stars and spaces.
- Conditionally Print Stars: Stars are printed on the middle row and the middle column of each row, while spaces are printed in the other positions.
C Program
#include <stdio.h>
int main() {
int i, j, size;
// Step 1: Accept the size of the pattern
printf("Enter the size (odd number) for the cross pattern: ");
scanf("%d", &size);
// Step 2: Outer loop for rows
for (i = 1; i <= size; i++) {
// Step 3: Inner loop for columns
for (j = 1; j <= size; j++) {
// Step 4: Print stars for the middle column and diagonal (forming the cross)
if (i == j || j == (size - i + 1)) {
printf("*");
} else {
printf(" ");
}
}
// Move to the next line after each row
printf("\n");
}
return 0;
}
Explanation
Step 1: Input Size
- The program starts by asking the user to input the size of the pattern. The size should be an odd number to create a symmetrical cross.
Step 2: Outer Loop for Rows
- The outer loop controls how many rows are printed. It runs from
1tosize.
Step 3: Inner Loop for Columns
- The inner loop controls the columns, printing stars or spaces.
Step 4: Conditional Printing
- Stars (
*) are printed in the middle column (i == j) and the opposite diagonal (j == size - i + 1). - Spaces are printed in all other positions to form the cross shape.
Output Example
For size = 5, the output will be:
* *
* *
*
* *
* *
For size = 7, the output will be:
* *
* *
* *
*
* *
* *
* *
Conclusion
This C program prints a cross-star pattern using nested loops and conditional logic. The program prints stars on the diagonals to form the cross shape while printing spaces in other positions. This exercise is useful for practicing loop control 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