C (programming language)

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

Introduction

C allows for creating visually captivating patterns using stars. One such captivating pattern is the inverted half right-side pyramid, formed entirely of stars. This guide delves into crafting a C program that achieves this pattern, exploring essential programming concepts like loops, conditionals, and character manipulation.

Understanding the Pattern

Imagine a triangle, standing tall on its left side but incomplete on its right. Instead of solid lines, this triangle is composed of neatly stacked rows of stars, decreasing in number as they descend. Here’s a simple representation:

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

Key Concepts Involved

  • Loops: The backbone of the program, loops will 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;
}

Customization and Enhancements

  • Vary the printed character instead of stars for artistic patterns.
  • Implement user input for the character to print.
  • Add colors or blinking effects using ANSI escape sequences.

Conclusion

With a grasp of loop control and conditional statements, C becomes a powerful tool for creating captivating visual patterns like the inverted half right-side pyramid. By understanding the logic behind the code and exploring its potential for customization, you can unleash your creativity and bring captivating patterns to life in the world of 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