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

  1. Header Inclusion:

C

#include <stdio.h>
  1. Main Function:

C

int main() {
  1. Variable Declaration:

C

int rows, i, j;
  1. User Input for Number of Rows:

C

printf("Enter the number of rows: ");
scanf("%d", &rows);
  1. Outer Loop for Rows:

C

for (i = rows; i >= 1; i--) { // Iterate in descending order for inverted shape
  1. Inner Loop for Stars:

C

for (j = 1; j <= i; j++) { // Print stars based on current row number
    printf("*");
}
  1. Newline for Next Row:

C

printf("\n");
  1. 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:

  1. stdio.h provides input/output functions.
  2. main is the program’s entry point.
  3. Variables rows, i, and j track row count, row iteration, and star printing.
  4. User input determines the number of rows.
  5. The outer loop iterates in descending order for the inverted pattern.
  6. The inner loop prints stars based on the current row number, forming the left-side pyramid.
  7. printf("\n") moves the cursor to the next line for each row.
  8. 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.

CodeForHunger

Learn coding the easy way. Find programming guides, examples and solutions with explanations.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button