📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 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 (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
The right arrow star pattern is a triangular star pattern that first grows in size and then shrinks to form an arrow pointing to the right. This exercise helps in understanding the use of nested loops and how to manipulate output using spaces and stars.
Problem Statement
Create a C program that:
- Accepts the number of rows for the right arrow pattern.
- Prints the right arrow star pattern.
Example:
- Input:
rows = 5
- Output:
*
**
***
****
*****
****
***
**
*
Solution Steps
- Input the Number of Rows: The size determines the number of rows for the upper triangle, and the lower part is symmetrical to the upper part.
- Use Nested Loops: The outer loops handle the rows, and the inner loops handle printing the stars and spaces.
- Display the Right Arrow Pattern: Print stars increasing for the upper part and decreasing for the lower part.
C Program
#include <stdio.h>
int main() {
int i, j, rows;
// Step 1: Accept the number of rows for the arrow pattern
printf("Enter the number of rows: ");
scanf("%d", &rows);
// Step 2: Print the upper part of the right arrow
for (i = 1; i <= rows; i++) {
// Print stars
for (j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
// Step 3: Print the lower part of the right arrow (inverted triangle)
for (i = rows - 1; i >= 1; i--) {
// Print stars
for (j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Explanation
Step 1: Input Number of Rows
- The program begins by asking the user to input the number of rows for the right arrow pattern.
Step 2: Print the Upper Part of the Right Arrow
- The outer loop controls how many rows are printed for the upper part.
- The inner loop prints stars (
*
) in increasing order from1
torows
.
Step 3: Print the Lower Part of the Right Arrow
- The second outer loop handles the rows for the lower part, which is the inverted version of the upper part.
- The inner loop prints stars in decreasing order.
Output Example
For rows = 5
, the output will be:
*
**
***
****
*****
****
***
**
*
For rows = 6
, the output will be:
*
**
***
****
*****
******
*****
****
***
**
*
Conclusion
This C program prints a right arrow star pattern by using nested loops to handle the increasing and decreasing number of stars. It is a useful exercise to practice working with loops, conditional statements, and formatting in C programming.
Comments
Post a Comment
Leave Comment