Open In App

C Program For Printing Half Right Number Pyramid Pattern

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

Here, we will see a C program to print the 3 different number patterns. There are 3 number patterns covered using for loop and while loop with their respective explanation.

3 Different Number Patterns: 

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

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

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

Pattern 1:

Input:

n = 5

Output:

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

A. Using for loop

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 number and increment the number to print the next number. 

C




// C program to print number pyramid patterns using for loop
#include <stdio.h>
 
int main()
{
 
    int rows, columns, number = 1, n = 5;
 
    // first for loop is used to identify number of rows
    for (rows = 0; rows <= n; rows++) {
 
        // second for loop is used to identify number of
        // columns and here the values will be changed
        // according to the first for loop
        for (columns = 0; columns < rows; columns++) {
 
            // printing number pattern based on the number
            // of columns
            printf("%d ", number);
 
            // incrementing number at each column to print
            // the next number
            number++;
        }
 
        // print the next line for each row
        printf("\n");
    }
    return 0;
}


Output

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

Time complexity: O(n*n)

Here n is given no of rows.

Auxiliary space: O(1)

As constant extra space is used.

B. 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




// C program to print number
// patterns using while loop
#include <stdio.h>
 
int main()
{
 
    int rows = 1, columns = 0, n = 5;
 
    // 1 value is assigned to the number
    // helpful to print the number pattern
    int number = 1;
 
    // while loops check the condition and repeat
    // the loop until the condition is false
    while (rows <= n) {
        while (columns <= rows - 1) {
 
            // printing number to get required pattern
            printf("%d ", number);
 
            // incrementing columns value
            columns++;
 
            // incrementing number value to
            // print the next number
            number++;
        }
        columns = 0;
 
        // incrementing rows value
        rows++;
        printf("\n");
    }
    return 0;
}


Output

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

Time complexity: O(n*n)

Here n is given no of rows.

Auxiliary space: O(1)

As constant extra space is used.

Pattern 2:

Input:

n = 5

Output:  

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

A. Using for loop

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 row number to get the required output.

C




// C program to print number
// patterns using for loop
#include <stdio.h>
 
int main()
{
    int rows, columns, n = 5;
 
    // first for loop is used to
    // identify number of rows
    for (rows = 1; rows <= n; rows++) {
 
        // second for loop is used to identify number of
        // columns and here the values will be changed
        // according to the first for loop
        for (columns = 1; columns <= rows; columns++) {
 
            // printing number pattern based on the number
            // of rows
            printf("%d ", rows);
        }
        // print the next line for each row
        printf("\n");
    }
    return 0;
}


Output

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

Time complexity: O(n*n)

Here n is given no of rows.

Auxiliary space: O(1)

As constant extra space is used.

B. 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




// C program to print number
// patterns using while loop
#include <stdio.h>
int main()
{
 
    int rows = 1, columns = 0, n = 5;
 
    // while loops check the condition and repeat
    // the loop until the condition is false
    while (rows <= n) {
 
        while (columns <= rows - 1) {
 
            // printing number to get required pattern
            printf("%d ", rows);
 
            // incrementing columns value
            columns++;
        }
        columns = 0;
 
        // incrementing rows value
        rows++;
 
        printf("\n");
    }
    return 0;
}


Output

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

Time complexity: O(n*n)

Here n is given no of rows.

Auxiliary space: O(1)

As constant extra space is used.

Pattern 3:

Input: 

n = 5

Output:

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

A. Using for loop:

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 column number to get the required output.

C




// C program to print number
// patterns using while loop
#include <stdio.h>
 
int main()
{
    int rows, columns, n = 5;
 
    // second for loop is used to identify number of rows
    for (rows = 1; rows <= n; rows++) {
 
        // second for loop is used to identify number of
        // columns and here the values will be changed
        // according to the first for loop
        for (columns = 1; columns <= rows; columns++) {
 
            // print the number to be print based on the
            // number of columns
            printf("%d ", columns);
        }
        // print the next line
        printf("\n");
    }
    return 0;
}


Output

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

Time complexity: O(n*n)

Here n is given no of rows.

Auxiliary space: O(1)

As constant extra space is used.

B. Using while loop

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

C




// C program to print number
// patterns using while loop
#include <stdio.h>
 
int main()
{
    int rows = 1, columns = 1, n = 6;
    int number = 1;
 
    // while loops check the condition and repeat
    // the loop until the condition is false
    while (rows <= n) {
 
        while (columns <= rows - 1) {
 
            // printing number to get required pattern
            printf("%d ", columns);
 
            // incrementing columns value
            columns++;
        }
        columns = 1;
 
        // incrementing rows value
        rows++;
        printf("\n");
    }
    return 0;
}


Output

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

Time complexity: O(n2) for given input n

Auxiliary space: O(1) 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads