C (programming language)

C program to print half left side of the pyramid star pattern

Understanding the Pattern

  • Visualize a pyramid standing upright, showcasing only its left half.
  • The pyramid’s structure is formed using stars, with each row containing an increasing number of stars.
  • The pattern resembles a triangle with its right side absent.

Key Concepts Involved

  • Loops: Utilize loops to iterate through rows and stars, creating the pattern’s repetitive structure.
  • Conditional Statements: Employ conditional statements to control the number of stars printed in each row, shaping the pyramid’s distinct form.
  • Character Printing: Use the printf function to display the stars on the console, forming the visual pattern.

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 = 1; i <= rows; i++) { // Iterate from 1 to rows
  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 = 1; i <= rows; i++) {
        for (j = 1; j <= i; j++) {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

Explanation:

  1. The stdio.h header file 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 to store the number of rows, row counter, and star counter, respectively.
  4. The user is prompted to enter the desired number of rows.
  5. The outer loop iterates from 1 to the specified number of rows, creating each row of the pyramid.
  6. The inner loop prints stars up to the current row number, forming the left-side pyramid shape.
  7. printf("\n") moves the cursor to the next line, starting a new row.
  8. return 0 indicates successful program execution.

Customization and Enhancements:

  • Change the printed character to create different patterns (e.g., numbers, letters, symbols).
  • Allow user input for the character to be printed.
  • Explore colors or blinking effects using ANSI escape sequences for visual appeal.

Conclusion:

By understanding loops, conditional statements, and character printing in C, you can create engaging visual patterns like the half left side of the pyramid star pattern. Experiment with different variations and customizations to expand your programming skills and create visually appealing outputs.

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