🎓 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 hollow right-angled triangle consists of stars (*) along the boundary of the triangle, with the interior filled with spaces. This pattern helps in understanding how to use loops and conditional statements to control the printing of stars and spaces.
Problem Statement
Create a C program that:
- Accepts the number of rows for the triangle.
- Prints a hollow right-angled triangle pattern using stars (
*).
Example:
- Input:
rows = 5 - Output:
* ** * * * * *****
Solution Steps
- Input the Number of Rows: The size determines how many rows the triangle will have.
- Use Nested Loops: The outer loop controls the rows, and the inner loop controls the printing of stars and spaces.
- Conditionally Print Stars: Stars are printed at the boundary (first row, last row, and first and last columns), while spaces are printed inside to create the hollow effect.
C Program
#include <stdio.h>
int main() {
int i, j, rows;
// Step 1: Accept the number of rows for the triangle
printf("Enter the number of rows: ");
scanf("%d", &rows);
// Step 2: Outer loop for rows
for (i = 1; i <= rows; i++) {
// Step 3: Inner loop for columns
for (j = 1; j <= i; j++) {
// Step 4: Print stars at the boundary or at the first or last column of each row
if (i == rows || j == 1 || j == i) {
printf("*");
} else {
printf(" "); // Print spaces inside the triangle
}
}
// Move to the next line after each row
printf("\n");
}
return 0;
}
Explanation
Step 1: Input Number of Rows
- The program starts by asking the user for the number of rows to print the hollow right-angled triangle.
Step 2: Outer Loop for Rows
- The outer loop controls the number of rows printed, running from
1torows.
Step 3: Inner Loop for Columns
- The inner loop controls the number of columns (stars) printed for each row.
Step 4: Conditional Printing
- Stars (
*) are printed at the boundary:- On the first row (
i == 1), - On the last row (
i == rows), - At the first column (
j == 1), - At the last column of each row (
j == i).
- On the first row (
- Spaces are printed for the inside of the triangle, creating the hollow effect.
Output Example
For rows = 5, the output will be:
*
**
* *
* *
*****
For rows = 6, the output will be:
*
**
* *
* *
* *
******
Conclusion
This C program prints a hollow right-angled triangle pattern using stars (*). The program uses nested loops and conditional logic to print stars along the boundary of the triangle while leaving the interior hollow. This exercise is useful for understanding loop control and conditional printing 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