C (programming language)
C program to print inverted half left side of pyramid star Pattern
Understanding the Pattern
- Imagine a pyramid standing on its tip, with only its left half visible.
- The pyramid is constructed of stars, with each row containing a decreasing number of stars.
- The pattern is inverted, meaning the rows are arranged from bottom to top, creating a downward-pointing shape.
Key Concepts Involved
- Loops: Iterate through rows and stars to create the pattern.
- Conditional Statements: Control the number of stars printed in each row.
- Character Printing: Use
printf
to display stars.
Step-by-Step Code Breakdown
- Header Inclusion:
C
#include <stdio.h>
- Main Function:
C
int main() {
- Variable Declaration:
C
int rows, i, j;
- User Input for Number of Rows:
C
printf("Enter the number of rows: ");
scanf("%d", &rows);
- Outer Loop for Rows:
C
for (i = rows; i >= 1; i--) { // Iterate in descending order for inverted shape
- Inner Loop for Stars:
C
for (j = 1; j <= i; j++) { // Print stars based on current row number
printf("*");
}
- Newline for Next Row:
C
printf("\n");
- Return Statement:
C
return 0;
}
Full Code:
C
#include <stdio.h>
int main() {
int rows, i, j;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; i--) {
for (j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Explanation:
stdio.h
provides input/output functions.main
is the program’s entry point.- Variables
rows
,i
, andj
track row count, row iteration, and star printing. - User input determines the number of rows.
- The outer loop iterates in descending order for the inverted pattern.
- The inner loop prints stars based on the current row number, forming the left-side pyramid.
printf("\n")
moves the cursor to the next line for each row.return 0
signals successful execution.
Customization and Enhancements:
- Change the printed character for different appearances.
- Allow user input for the character to print.
- Incorporate colors or blinking effects using ANSI escape sequences.
Conclusion:
By mastering loops and conditional statements, you can create various visual patterns like the inverted half left-side pyramid star pattern. Understanding the logic behind the code and exploring customization options allows for creative pattern design in C programming.