Open In App

C Program For Printing Simple Half Right Star Pyramid Pattern

Last Updated : 26 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Here, we will develop a C Program To Print Simple Half Right Star Pyramid Pattern using two approaches i.e.

  1. Using for loop 
  2. Using while loop

Input:

rows = 5

Output:

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

1. Using for loop:

First for loop is used to identify the number of rows and the second for loop is used to identify the number of columns. Here the values will be changed according to the first for loop

C




// C program to print simple pyramid pattern
#include <stdio.h>
 
int main()
{
 
    int rows = 5;
 
    // first for loop is used to identify number of rows
    for (int i = 1; i <= rows; i++) {
 
        // second for loop is used to identify number of
        // columns and here the values will be changed
        // according to the first for loop
        for (int j = 1; j <= i; j++) {
 
            // printing the required pattern
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}


Output

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

Time complexity: O(n*n)

Here n is given no of rows.

Auxiliary space: O(1)

As constant extra space is used.

2. Using while loop

The while loops check the condition until the condition is false. If the condition is true then enter into the loop and execute the statements. 

C++




#include <stdio.h>
int main()
{
    int i = 0, j = 0;
    int rows = 5;
 
    // while loop check the condition until the given
    // condition is false if it is true then enteres in to
    // the loop
    while (i < rows) {
 
        // this loop will print the pattern
        while (j <= i) {
            printf("* ");
            j++;
        }
        j = 0;
        i++;
        printf("\n");
    }
    return 0;
}


Output

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

Time complexity: O(n2) where n is given input rows

Auxiliary space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads