Create a pyramid using star(*) |C Program|

Introduction

     Creating patterns using programming is a fun and educational way to understand loops and control structures. This C program generates a pyramid pattern of stars based on user input. The blog post will walk through the logic behind creating such patterns and explain the step-by-step implementation of the provided program.     
  
Pyramid Patterns
Pyramid patterns involve printing characters or symbols in a way that forms a pyramid shape.
These patterns are commonly used in programming exercises to enhance logical and looping skills.


Code

    
     
    #include<stdio.h>
    #include<conio.h>

    void main() {
        int i, space, rows, k = 0;
        clrscr();
    
        // Input
        printf("Enter number of rows: ");
        scanf("%d", &rows);
    
        // Printing Pyramid Pattern
        for (i = 1; i <= rows; ++i, k = 0) {
            for (space = 1; space <= rows - i; ++space) {
                printf(" ");
            }
    
            while (k != 2 * i - 1) {
                printf("*");
                ++k;
            }
    
            printf("\n");
        }
    
        getch();
    }
       
     


Output


    Enter number of rows: 5
        *
       ***
      *****
     *******
    *********
       

In this example, the user inputs the number of rows as 5. The program then prints a pyramid pattern of stars with the specified number of rows.

You can try running the program with different numbers of rows to observe how it dynamically adjusts and prints the corresponding pyramid pattern. The output will display a pyramid structure formed by stars.

Explanation

1. Header Files:
    The program includes the standard input/output header <stdio.h> and the console input/output header <conio.h>.
2. Main Function:
    The main function serves as the entry point of the program.
3. Input:
    Users input the number of rows for the pyramid.
4. Printing Pyramid Pattern:
    The program uses nested loops to control the printing of spaces and stars, forming a pyramid pattern.
The outer loop (for (i = 1; i <= rows; ++i, k = 0)) manages the rows, and the inner loops handle spaces and stars accordingly.
5. Output:
    The program outputs the pyramid pattern of stars based on user input.Conclusion


Conclusion

     This C program provides a practical example of printing a pyramid pattern using nested loops. Understanding and implementing such patterns is a fundamental skill for programming logic. Feel free to run the program with different numbers of rows to observe how it dynamically adjusts and prints the corresponding pyramid pattern. This example serves as a valuable introduction to creating pyramid patterns in C. Happy coding!

Post a Comment

0 Comments