Open In App

C Program To Print Character Pyramid Pattern

Improve
Improve
Like Article
Like
Save
Share
Report

Here, we will build a C Program To Print Character Pyramid Pattern using 2 approaches i.e.

  1. Using for loop
  2. Using while loop

Input:

rows = 5

Output:

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

1. Using for loop

Approach 1: Assign any character to one variable for the printing pattern

The first for loop is used to iterate the number of rows and the second for loop is used to repeat the number of columns. Then print the character based on the number of columns. 

Example:

C




// C program to print character pattern using character
 
#include <stdio.h>
 
int main()
{
    int i, j;
 
    // input entering number of rows
    int rows = 5;
 
    // taking first character of alphabet
    // which is useful to print pattern
    char character = 'A';
 
    // first for loop is used to identify number rows
    for (i = 0; i < rows; i++) {
 
        // second for loop is used to identify number
        // of columns based on the rows
        for (j = 0; j <= i; j++) {
 
            // printing character to get the required
            // pattern
            printf("%c ", character);
        }
        printf("\n");
 
        // incrementing character value so that it
        // will print the next character
        character++;
    }
    return 0;
}


Output

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

Time complexity: O(R*R)

Here R is given no of rows.

Auxiliary space: O(1)

As constant extra space is used.

Approach 2: Printing pattern by converting a given number into a character

Assign any number to one variable for the printing pattern. The first for loop is used to iterate the number of rows and the second for loop is used to repeat the number of columns. After entering into the loop convert the given number into character to print the required pattern based on the number of columns.

Example:

C




// C program to print character pattern by
// converting number in to character
#include <stdio.h>
 
int main()
{
    int i, j;
 
    // input entering number of rows
    int rows = 5;
 
    // given a number
    int number = 65;
 
    // first for loop is used to identify number rows
    for (i = 0; i < rows; i++) {
 
        // second for loop is used to identify number
        // of columns based on the rows
        for (j = 0; j <= i; j++) {
 
            // converting number in to character
            char character = (char)(number);
 
            // printing character to get the required
            // pattern
            printf("%c ", character);
        }
        printf("\n");
 
        // incrementing number value so that it
        // will print the next character
        number++;
    }
    return 0;
}


Output

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

Time complexity: O(R*R)

Here R is given no of rows.

Auxiliary space : O(1)

As constant extra space is used.

2. Using while loops:

Approach 1: 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.

Example:

C




// C program to print character pattern by
// converting number into character
#include <stdio.h>
 
int main()
{
    int i = 1, j = 0;
 
    // input entering number of rows
    int rows = 5;
 
    // given a character
    char character = 'A';
 
    // while loops checks the conditions until the
    // condition is false if condition is true then enters
    // in to the loop and executes the statements
    while (i <= rows) {
        while (j <= i - 1) {
 
            // printing character to get the required
            // pattern
            printf("%c ", character);
            j++;
        }
        printf("\n");
 
        // incrementing character value so that it
        // will print the next character
        character++;
        j = 0;
        i++;
    }
    return 0;
}


Output

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

Time complexity: O(R*R) 

Here R is given no of rows.

Auxiliary space : O(1)

As constant extra space is used.

Approach 2:  Printing pattern by converting given number into character using while loop

Example:

C




// C program to print character pattern by
// converting number in to character
#include <stdio.h>
 
int main()
{
    int i = 1, j = 0;
 
    // input entering number of rows
    int rows = 5;
 
    // given a number
    int number = 65;
 
    // while loops checks the conditions until the
    // condition is false if condition is true then enters
    // in to the loop and executes the statements
    while (i <= rows) {
        while (j <= i - 1) {
 
            // converting number in to character
            char character = (char)(number);
 
            // printing character to get the required
            // pattern
            printf("%c ", character);
            j++;
        }
        printf("\n");
 
        // incrementing number value so that it
        // will print the next character
        number++;
        j = 0;
        i++;
    }
    return 0;
}


Output

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

Time complexity: O(R*R) where R is given no of rows

Auxiliary space : O(1)



Last Updated : 26 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads