C (programming language)

C program to print inverted half right side of pyramid star Pattern

Introduction

In the realm of C programming, creating visual patterns with stars is a captivating way to explore programming concepts. This guide delves into crafting a C program that generates an inverted half right-side pyramid star pattern, fostering understanding of loops, conditional statements, and character manipulation.

Visualizing the Pattern:

        *
       **
      ***
     ****
    *****

Key Concepts Involved:

  • Loops: The backbone of the program, loops iterate to print each row and star within it.
  • Conditional Statements: ifelse statements control the number of stars printed in each row, shaping the pyramid.
  • Character Printing: We’ll use printf with the * character to directly print 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--) {
  1. Inner Loop for Spaces:

C

for (j = 1; j <= rows - i; j++) {
    printf(" "); // Prints spaces to offset the pyramid
}
  1. Inner Loop for Stars:

C

for (j = 1; j <= 2 * i - 1; j++) {
    printf("*"); // Prints stars based on double the current row number
}
  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 <= rows - i; j++) {
            printf(" ");
        }
        for (j = 1; j <= 2 * i - 1; j++) {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

Explanation:

  1. The #include <stdio.h> header provides input/output functions like printf and scanf.
  2. The main function is the program’s entry point.
  3. Variables rows, i, and j are used for row count, row iteration, and star printing, respectively.
  4. The user enters the desired number of rows.
  5. The outer loop iterates from the highest row to the first, controlling the downward shape of the pyramid.
  6. The first inner loop prints spaces to create the pyramid’s offset.
  7. The second inner loop prints stars based on the current row number, forming the pyramid’s body.
  8. printf("\n") moves the cursor to the next line for the next row.
  9. return 0; indicates successful program execution.

Customization and Enhancements:

  • Change the printed character for artistic patterns.
  • Allow user input for the character to print.
  • Add colors or blinking effects using ANSI escape sequences.

Conclusion:

By mastering loops and conditional statements in C, you can create diverse visual patterns like the inverted half right-side pyramid star pattern. Understanding the logic behind the code and exploring potential customizations unleashes creativity in C programming, bringing captivating visual elements to life.

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