Pyramid Patterns in C

Pyramid Patterns in C


 Program to print right angle pyramid using *

So in this pattern, we will declare three int variables first, 2 variables are used for loops while 1 variable is to get a number of printing rows from the user, I have explained each line of code with comments, have a look.

#include <stdio.h>
int main()
{
    //declare variables to use
    int i, j, rows;
    
    //Enter the number of rows you want to print, printf shows this command to user
    printf("Enter number of rows: ");
  
    //scanf is used to get value, and save that value in 'rows' variable
    scanf("%d",&rows);

    //now loop for each row
    for(i=1; i<=rows; ++i)
    {
        //this loop will print * until value of outer loop variable(i) is greater than 
        //inner loop vairable(j)
        for(j=1; j<=i; ++j)
        {
            printf("* ");
        }
        //as soon as inner loop(j) value becomes greater than outer loop printing * ends
       // & it prints new line using \n
        printf("\n");
    }
    return 0;
}

Executing the above code will give you following output

Enter number of rows: 6                                        
*  
* *
* * * 
* * * * 
* * * * * 
* * * * * *                        

Here the working link for the online demo https://www.onlinegdb.com/ry41pFlSG