Open In App

C Program to Print Number Pattern

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

A number pattern involves printing numbers in a specific arrangement or shape, often in the form of a pyramid, triangle, or other geometric shapes. They are great for practicing loops and conditional statements. In this article, we will learn how to print different number patterns in C.


Rhombus Number Pattern

The Rhombus Number Pattern pattern looks like a rhombus shape which is nothing but a slightly slanted square filled with numbers starting from 1 to the given size of the edge.

C
#include <stdio.h>

int main() {
    int n = 5;

    // Outer loop to iterate through each row
    for (int i = 0; i < n; i++) {

        // First inner loop to print white spaces
        for (int j = 0; j < n - i - 1; j++)
            printf("  ");

        // Second inner loop to print number in each row
        for (int k = 0; k < n; k++)
            printf("%d ", k + 1);
        printf("\n");
    }
    return 0;
}

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

Right Half Pyramid Pattern

The Right Half Pyramid Pattern pattern forms a right-angled triangle with numbers increasing along each row and aligned to the left.

C
#include <stdio.h>

int main() {
    int n = 5;

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

        // Second loop for printing number 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 

Left Half Pyramid Pattern

The Left Half Pyramid Pattern pattern is similar to the right half pyramid but the numbers are aligned to the left.

C
#include <stdio.h>

int main() {
    int n = 5;

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

        // First inner loop for printing leading whitespaces
        for (int j = 0; j < 2 * (n - i) - 2; j++)
            printf(" ");

        // Second inner loop for printing  numbers
        for (int k = 0; k <= i; k++)
            printf("%d ", k + 1);
        printf("\n");
    }
    return 0;
}

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

 Inverted Right Half Pyramid

An Inverted Right Half Pyramid Pattern is a variation of right half triangle pattern flipped 180 vertically.

C
#include <stdio.h>

int main() {
    int n = 5;

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

        // Inner loop to print the numbers in each row
        for (int j = 0; j < n - i; j++)
            printf("%d ", j + 1);
        printf("\n");
    }
}

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

Inverted Left Half Pyramid Pattern

An Inverted left Half Pyramid Pattern is a variation of left half triangle pattern flipped 180 vertically.

C
#include <stdio.h>

int main() {
    int n = 5;

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

        // First inner loop for printing white spaces
        for (int j = 0; j < 2 * i; j++)
            printf(" ");

        // Second inner loop for printing numbers
        for (int k = 0; k < n - i; k++)
            printf("%d ", k + 1);
        printf("\n");
    }

    return 0;
}

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

 Full Pyramid Pattern

A Full Pyramid Pattern consists of numbers arranged in a symmetric equilateral triangular shape. The largest row is at the bottom, and each row above it contains fewer numbers.

C
#include <stdio.h>

int main() {
    int n = 5;

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

        // First inner loop to print white spaces
        for (int j = 0; j < 2 * (n - i) - 1; j++)
            printf(" ");

        // Second inner loop to print numbers
        for (int k = 0; k < 2 * i + 1; k++)
            printf("%d ", k + 1);
        printf("\n");
    }
    return 0;
}

Output
         1 
       1 2 3 
     1 2 3 4 5 
   1 2 3 4 5 6 7 
 1 2 3 4 5 6 7 8 9 

Inverted Full Pyramid Pattern

An Inverted Full Pyramid Pattern is a variation of full pyramid pattern rotated upside down.

C
#include <stdio.h>

int main() {
    int n = 5;

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

        // First inner loop for printing leading white
        // spaces
        for (int j = 0; j < 2 * i; j++)
            printf(" ");

        // Second inner loop for printing numbers
        for (int k = 0; k < 2 * (n - i) - 1; k++)
            printf("%d ", k + 1);
        printf("\n");
    }
}

Output
1 2 3 4 5 6 7 8 9 
  1 2 3 4 5 6 7 
    1 2 3 4 5 
      1 2 3 
        1 

Pascal’s Triangle Pattern

Pascal’s triangle is a triangular array of numbers where each number is the sum of the two numbers directly above it. The numbers generally are center aligned.

C
#include <stdio.h>

int main() {
    int n = 5;

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

        // First inner loop for leading white spaces
        for (int j = 0; j < n - i; j++)
            printf(" ");

        // Coefficient
        int C = 1; 

        // Second inner loop for printing numbers
        for (int k = 1; k <= i; k++) {
            printf("%d ", C);
            C = C * (i - k) / k;
        }
        printf("\n");
    }
    return 0;
}

Output
    1 
   1 1 
  1 2 1 
 1 3 3 1 
1 4 6 4 1 

Floyd’s Triangle Pattern

Floyd’s Triangle is a right-angled triangular pattern of numbers, where the numbers are printed sequentially across rows. Each row contains increasing numbers, starting from 1 in the first row, 2,3in the second row, and so on, until all numbers from 1 to n (the total count) are printed.

C
#include <stdio.h>

int main() {
    int n = 4;
    int c = 1;

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

        // Inner loop to print abphabet in each row
        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 


Next Article

Similar Reads