Open In App

C Program For Printing Right Half Pyramid Pattern

Last Updated : 15 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

A half-right pyramid consists of rows with sequential stars, numbers or characters arranged in a triangular shape. The first row has one character, the second row has two, and so on. The characters are aligned to the left making it similar to the right-angle triangle.

Right-Half-Pyramid-Pattern

In this article, we will learn how to print different right-half pyramid patterns using C program.

The patterns are printed by visualizing the output space as a matrix with rows and columns and then printing the characters or whitespace according to the shape we want. Let’s take a look at an example for right-half pyramid pattern:

C
#include <stdio.h>

int main() {
    int n = 5;

    // Outer loop for printing rows
    for (int i = 0; i < n; i++) {

        // Inner loop for printing * in each rows
        for (int j = 0; j <= i; j++)
            printf("* ");
        printf("\n");
    }
    return 0;
}

Output
* 
* * 
* * * 
* * * * 
* * * * * 

Explanation: Here, n is the number of rows to be printed.

  • Outer loop print rows. It runs n times (0 to n-1) creating a new row in each iteration.
  • Inner loop prints the star in each row. It runs from j = 0 up to the current value of i. This means:
    • When i is 0, the inner loop runs once printing one star.
    • When i is 1, the inner loop runs twice printing two star.
    • And so on…
  • The inner loop’s dependency on the outer loop’s variable (j <= i) is what creates the increasing number of stars per row, forming the triangle.

Any shape that resembles the above pattern can be called a right half pyramid. Due to this, there can be different variations of this pattern based on the characters used to print it.

Note: We can use other loops too but using for loop is most suitable for this type of problems.

Other Right Half Pyramid Pattern

Following are some of the variations of right half pyramid pattern:

Right Half Pyramid of a Number

The pattern consists of rows where each row contains the same number repeated.

C
#include <stdio.h>

int main() {
    int n = 5;

    // Outer loop for printing rows
    for (int i = 0; i < n; i++) {

        // Inner loop for printing 2 in each rows
        for (int j = 0; j <= i; j++)
            printf("2 ");
        printf("\n");
    }
    return 0;
}

Output
2 
2 2 
2 2 2 
2 2 2 2 
2 2 2 2 2 

Right Half Pyramid of Row Numbers

In this pattern, each row is made up of its own row number. In this program, we are using while loop instead of for loop.

C
#include <stdio.h>

int main() {
    int n = 5;
    int i = 0;

    // Outer loop for printing rows
    while (i < n) {
        int j = 0;

        // Inner loop for printing numbers in each row
        while (j <= i) {
            printf("%d ", i + 1);
            j++;
        }
        printf("\n");
        i++;
    }
    return 0;
}

Output
1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5 

Right Half Pyramid of Consecutive Numbers in Each Row

Each row begins with 1 and increments by one for each consecutive number in that row.

C
#include <stdio.h>

int main() {
    int n = 5;

    // Outer loop for printing rows
    for (int i = 0; i < n; i++) {

        // Inner loop for printing numbers in each rows
        for (int j = 0; j <= i; j++)
            printf("%d ", j + 1);
        printf("\n");
    }
    return 0;
}

Output
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 

Right Half Pyramid of Continious Numbers

This right half pyramid is made up of consecutive numbers starting from 1 and keeps incrementing till the end of the pattern is reached.

C
#include <stdio.h>

int main() {
    int n = 5;

  	int c = 1;
    // Outer loop for printing rows
    for (int i = 0; i < n; i++) {

        // Inner loop for printing numbers in each rows
        for (int j = 0; j <= i; j++)
            printf("%d ", c++);
        printf("\n");
    }
    return 0;
}

Output
1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 

All these pyramids patterns can also be created using alphabets.

Right Half Pyramid of Alphabets

This pattern is the alphabet version of the right half pyramid with row numbers. ‘A’ is just added to the loop variable to align it with the ASCII values of uppercase alphabets.

C
#include <stdio.h>

int main() {
    int n = 5;

    // Outer loop for printing rows
    for (int i = 0; i < n; i++) {

        // Inner loop for printing alphabet in each rows
        for (int j = 0; j <= i; j++)
            printf("%c ", i + 'A');
        printf("\n");
    }
    return 0;
}

Output
A 
B B 
C C C 
D D D D 
E E E E E 

Right Half Pyramid of Consecutive Numbers in Each Row

This pattern is also the most common alphabetical right half pyramid pattern. The do while loop is being used here for demonstration.

C
#include <stdio.h>

int main() {
    int n = 5;
    int i = 0;

    // Outer loop for printing rows
    do {
        int j = 0;

        // Inner loop for printing characters in each row
        do {
            printf("%c ", j + 'A');
            j++;
        } while (j <= i);

        printf("\n");
        i++;
    } while (i < n);

    return 0;
}

Output
A 
A B 
A B C 
A B C D 
A B C D E 


Next Article

Similar Reads