The Wayback Machine - https://web.archive.org/web/20241109075623/https://www.geeksforgeeks.org/pattern-programs-in-c/
Open In App

Pattern Programs in C

Last Updated : 18 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Printing patterns using C programs has always been an interesting problem domain. We can print different patterns like star patterns, pyramid patterns, Floyd’s triangle, Pascal’s triangle, etc. in C language. These problems generally require the knowledge of loops and if-else statements.

In this article, we will discuss the following example programs for printing patterns in the C programming language.

pattern-program-in-c

Pattern Programs in C

If you’re looking to deepen your understanding of loops and how they are applied in data structures, the C Programming Course Online with Data Structures provides extensive exercises and examples.

1. Right Half Pyramid Pattern in C

The right-half pyramid is nothing but a right-angle triangle whose hypotenuse is in the right direction. We can print the right half pyramid pattern using numbers, alphabets, or any other character like a star (*).

Example:

C
// C program to print right half pyramid pattern of star
#include <stdio.h>

int main()
{
    int rows = 5;

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

        // second loop for printing character in each rows
        for (int j = 0; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}
C
// C program to print right half pyramid pattern of number
#include <stdio.h>

int main()
{
    int rows = 5;

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

        // second loop for printing number in each rows
        for (int j = 0; j <= i; j++) {
            printf("%d ", j + 1);
        }
        printf("\n");
    }
    return 0;
}
C
// C program to print right half pyramid pattern of
// alphabets
#include <stdio.h>

int main()
{
    int rows = 5;

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

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


Output

*           |   1           |   A
* * | 1 2 | A B
* * * | 1 2 3 | A B C
* * * * | 1 2 3 4 | A B C D
* * * * * | 1 2 3 4 5 | A B C D E

Time Complexity: O(rows*rows) as nested for loop is being run.
Auxiliary Space: O(1) as no extra space has been used.

2. Left Half Pyramid Pattern in C

The Left Half Pyramid looks like a right-angled triangle with its hypotenuse facing the left. We can also print this pattern using a character, alphabets, or numbers.

Example:

C
// c program to print left half pyramid pattern of star
#include <stdio.h>

int main()
{
    int rows = 5;

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

        // loop for printing leading whitespaces
        for (int j = 0; j < 2 * (rows - i) - 1; j++) {
            printf(" ");
        }

        // loop for printing * character
        for (int k = 0; k <= i; k++) {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}
C
// c program to print left half pyramid pattern of numbers
#include <stdio.h>

int main()
{
    int rows = 5;

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

        // loop for printing leading whitespaces
        for (int j = 0; j < 2 * (rows - i) - 2; j++) {
            printf(" ");
        }

        // loop for printing continious numbers
        for (int k = 0; k <= i; k++) {
            printf("%d ", k + 1);
        }
        printf("\n");
    }
    return 0;
}
C
// c program to print left half pyramid pattern of alphabets
#include <stdio.h>

int main()
{
    int rows = 5;

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

        // loop for printing leading whitespaces
        for (int j = 0; j < 2 * (rows - i) - 1; j++) {
            printf(" ");
        }

        // loop for printing alphabets
        for (int k = 0; k <= i; k++) {
            printf("%c ", 'A' + k);
        }
        printf("\n");
    }
    return 0;
}


Output

        *    |            1    |            A
* * | 1 2 | A B
* * * | 1 2 3 | A B C
* * * * | 1 2 3 4 | A B C D
* * * * * | 1 2 3 4 5 | A B C D E

Time Complexity: O(rows*rows) as nested for loop is being run.
Auxiliary Space: O(1) as no extra space has been used.

3. Full Pyramid Pattern in C

The Full Pyramid pattern looks similar to the Equilateral triangle. We can see this as the combination of the Left Half and Right Half pyramids patterns. The following example demonstrates how to print this pattern using alphabets, numbers, or a star (*).

Example:

C
// C program to print the full pyramid pattern of stars
#include <stdio.h>

int main()
{
    int rows = 5;

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

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

        // inner loop 2 to print star * character
        for (int k = 0; k < 2 * i + 1; k++) {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}
C
// C program to print the full pyramid pattern of alphabets
#include <stdio.h>

int main()
{
    int rows = 5;

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

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

        // inner loop 2 to print alphabets
        for (int k = 0; k < 2 * i + 1; k++) {
            printf("%c ", 'A' + k);
        }
        printf("\n");
    }
    return 0;
}
C
// C program to print the full pyramid pattern of numbers
#include <stdio.h>

int main()
{
    int rows = 5;

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

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

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


Output

         *           |           1           |           A
* * * | 1 2 3 | A B C
* * * * * | 1 2 3 4 5 | A B C D E
* * * * * * * | 1 2 3 4 5 6 7 | A B C D E F G
* * * * * * * * * | 1 2 3 4 5 6 7 8 9 | A B C D E F G H I

Time Complexity: O(rows*rows) as nested for loop is being run.
Auxiliary Space: O(1) as no extra space has been used.

4. Inverted Right Half Pyramid Pattern in C

This pattern is the 180° flipped version of the Right Half Pyramid Pattern we discussed earlier.

Example:

C
// C program to print the inverted right half pyramid of
// stars
#include <stdio.h>

int main()
{
    int rows = 5;

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

        // first inner loop to print the * in each row
        for (int j = 0; j < rows - i; j++) {
            printf("* ");
        }
        printf("\n");
    }
}
C
// C program to print the inverted right half pyramid of
// numbers
#include <stdio.h>

int main()
{
    int rows = 5;

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

        // first inner loop to print the numbers in each row
        for (int j = 0; j < rows - i; j++) {
            printf("%d ", j + 1);
        }
        printf("\n");
    }
}
C
// C program to print the inverted right half pyramid of
// alphabets
#include <stdio.h>

int main()
{
    int rows = 5;

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

        // first inner loop to print the alphabets in each
        // row
        for (int j = 0; j < rows - i; j++) {
            printf("%c ", 'A' + j);
        }
        printf("\n");
    }
}


Output

* * * * *   |   1 2 3 4 5   |   A B C D E 
* * * * | 1 2 3 4 | A B C D
* * * | 1 2 3 | A B C
* * | 1 2 | A B
* | 1 | A

Time Complexity: O(rows*rows) as nested for loop is being run.
Auxiliary Space: O(1) as no extra space has been used.

5. Inverted Left Half Pyramid Pattern in C

This pattern is the 180° flipped version of the left half pyramid pattern we discussed earlier.

Example:

C
// C program to print the inverted left half pyramid pattern
// of stars
#include <stdio.h>

int main()
{
    int rows = 5;

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

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

        // second inner loop for printing star *
        for (int k = 0; k < rows - i; k++) {
            printf("* ");
        }
        printf("\n");
    }

    return 0;
}
C
// C program to print the inverted left half pyramid pattern
// of numbers
#include <stdio.h>

int main()
{
    int rows = 5;

    // first loop for printing all rows
    for (int i = 0; i < rows; 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 < rows - i; k++) {
            printf("%d ", k + 1);
        }
        printf("\n");
    }

    return 0;
}
C
// C program to print the inverted left half pyramid pattern
// of alphabets
#include <stdio.h>

int main()
{
    int rows = 5;

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

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

        // second inner loop for printing alphabets
        for (int k = 0; k < rows - i; k++) {
            printf("%c ", 'A' + k);
        }
        printf("\n");
    }

    return 0;
}


Output

* * * * *    |    1 2 3 4 5    |    A B C D E
* * * * | 1 2 3 4 | A B C D
* * * | 1 2 3 | A B C
* * | 1 2 | A B
* | 1 | A

Time Complexity: O(rows*rows) as nested for loop is being run.
Auxiliary Space: O(1) as no extra space has been used.

6. Inverted Full Pyramid Pattern in C

It is a 180° flipped version of the Full Pyramid Pattern we discussed earlier. We can see this as the combination of the Inverted Left Half and Inverted Right Half Pyramid Pattern we discussed earlier.

Example:

C
// C program to print the inverted full pyramid pattern of
// stars
#include <stdio.h>

int main()
{
    int rows = 5;

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

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

        // second inner loop for printing stars *
        for (int k = 0; k < 2 * (rows - i) - 1; k++) {
            printf("* ");
        }
        printf("\n");
    }
}
C
// C program to print the inverted full pyramid pattern of
// numbers
#include <stdio.h>

int main()
{
    int rows = 5;

    // first loop for printing all rows
    for (int i = 0; i < rows; 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 * (rows - i) - 1; k++) {
            printf("%d ", k + 1);
        }
        printf("\n");
    }
}
C
// C program to print the inverted full pyramid pattern of
// alphabets
#include <stdio.h>

int main()
{
    int rows = 5;

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

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

        // second inner loop for printing alphabets
        for (int k = 0; k < 2 * (rows - i) - 1; k++) {
            printf("%c ", 'A' + k);
        }
        printf("\n");
    }
}


Output

* * * * * * * * *   |   1 2 3 4 5 6 7 8 9   |   A B C D E F G H I
* * * * * * * | 1 2 3 4 5 6 7 | A B C D E F G
* * * * * | 1 2 3 4 5 | A B C D E
* * * | 1 2 3 | A B C
* | 1 | A

Time Complexity: O(rows*rows) as nested for loop is being run.
Auxiliary Space: O(1) as no extra space has been used.

7. Rhombus Pattern in C

The Rhombus pattern is similar to the square pattern, just that we have to add spaces before each line and their count decreases progressively with rows.

Example:

C
// C Program to print the rhombus pattern using * star
#include <stdio.h>

int main()
{
    int rows = 5;

    // first outer loop to iterate through each row
    for (int i = 0; i < rows; i++) {

        // first inner loop to print white spaces
        for (int j = 0; j < rows - i - 1; j++) {
            printf(" ");
        }

        // second inner loop to print * star in each row
        for (int k = 0; k < rows; k++) {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}
C
// C Program to print the rhombus pattern using numbers
#include <stdio.h>

int main()
{
    int rows = 5;

    // first outer loop to iterate through each row
    for (int i = 0; i < rows; i++) {

        // first inner loop to print white spaces
        for (int j = 0; j < rows - i - 1; j++) {
            printf(" ");
        }

        // second inner loop to print number in each row
        for (int k = 0; k < rows; k++) {
            printf("%d ", k + 1);
        }
        printf("\n");
    }
    return 0;
}
C
// C Program to print the rhombus pattern using alphabet
#include <stdio.h>

int main()
{
    int rows = 5;

    // first outer loop to iterate through each row
    for (int i = 0; i < rows; i++) {

        // first inner loop to print white spaces
        for (int j = 0; j < rows - i - 1; j++) {
            printf(" ");
        }

        // second inner loop to print alphabet in each row
        for (int k = 0; k < rows; k++) {
            printf("%c ", k + 'A');
        }
        printf("\n");
    }
    return 0;
}


Output

    * * * * *   |       1 2 3 4 5   |       A B C D E
* * * * * | 1 2 3 4 5 | A B C D E
* * * * * | 1 2 3 4 5 | A B C D E
* * * * * | 1 2 3 4 5 | A B C D E
* * * * * | 1 2 3 4 5 | A B C D E

8. Diamond Pattern in C

The Diamond Pattern is obtained by joining the Full Pyramid and Inverted Full Pyramid Pattern by their bases. We can also print this pattern using any character.

Example:

C
// C Program to print diamond pattern using star *
#include <stdio.h>
int main()
{
    int n = 5;

    // first outer loop to iterate through each row
    for (int i = 0; i < 2 * n - 1; i++) {

        // assigning values to the comparator according to
        // the row number
        int comp;
        if (i < n) {
            comp = 2 * (n - i) - 1;
        }
        else {
            comp = 2 * (i - n + 1) + 1;
        }

        // first inner loop to print leading whitespaces
        for (int j = 0; j < comp; j++) {
            printf(" ");
        }

        // second inner loop to print stars *
        for (int k = 0; k < 2 * n - comp; k++) {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}
C
// C Program to print diamond pattern using numbers
#include <stdio.h>
int main()
{
    int n = 5;

    // first outer loop to iterate through each row
    for (int i = 0; i < 2 * n - 1; i++) {

        // assigning values to the comparator according to
        // the row number
        int comp;
        if (i < n) {
            comp = 2 * (n - i) - 1;
        }
        else {
            comp = 2 * (i - n + 1) + 1;
        }

        // first inner loop to print leading whitespaces
        for (int j = 0; j < comp; j++) {
            printf(" ");
        }

        // second inner loop to print number
        for (int k = 0; k < 2 * n - comp; k++) {
            printf("%d ", k + 1);
        }
        printf("\n");
    }
    return 0;
}
C
// C Program to print diamond pattern using alphabets
#include <stdio.h>
int main()
{
    int n = 5;

    // first outer loop to iterate through each row
    for (int i = 0; i < 2 * n - 1; i++) {

        // assigning values to the comparator according to
        // the row number
        int comp;
        if (i < n) {
            comp = 2 * (n - i) - 1;
        }
        else {
            comp = 2 * (i - n + 1) + 1;
        }

        // first inner loop to print leading whitespaces
        for (int j = 0; j < comp; j++) {
            printf(" ");
        }

        // second inner loop to print alphabet
        for (int k = 0; k < 2 * n - comp; k++) {
            printf("%c ", k + 'A');
        }
        printf("\n");
    }
    return 0;
}


Output

        *           |           1           |           A
* * * | 1 2 3 | A B C
* * * * * | 1 2 3 4 5 | A B C D E
* * * * * * * | 1 2 3 4 5 6 7 | A B C D E F G
* * * * * * * * * | 1 2 3 4 5 6 7 8 9 | A B C D E F G H I
* * * * * * * | 1 2 3 4 5 6 7 | A B C D E F G
* * * * * | 1 2 3 4 5 | A B C D E
* * * | 1 2 3 | A B C
* | 1 | A

9. Hourglass Pattern in C

Hourglass Pattern is a combination of the inverted full pyramid and full pyramid patterns but in the opposite sense to that of diamond pattern. Here we join them using their tip.

Example:

C
// C Program to print hourglass pattern using star *
#include <stdio.h>
int main()
{
    int rows = 5;

    // first outer loop to iterate each row
    for (int i = 0; i < 2 * rows - 1; i++) {

        // assigning comparator
        int comp;
        if (i < rows) {
            comp = 2 * i + 1;
        }
        else {
            comp = 2 * (2 * rows - i) - 3;
        }

        // first inner loop to print leading spaces
        for (int j = 0; j < comp; j++) {
            printf(" ");
        }

        // second inner loop to print star *
        for (int k = 0; k < 2 * rows - comp; k++) {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}
C
// C Program to print hourglass pattern using numbers
#include <stdio.h>
int main()
{
    int rows = 5;

    // first outer loop to iterate each row
    for (int i = 0; i < 2 * rows - 1; i++) {

        // assigning comparator
        int comp;
        if (i < rows) {
            comp = 2 * i + 1;
        }
        else {
            comp = 2 * (2 * rows - i) - 3;
        }

        // first inner loop to print leading spaces
        for (int j = 0; j < comp; j++) {
            printf(" ");
        }

        // second inner loop to print number
        for (int k = 0; k < 2 * rows - comp; k++) {
            printf("%d ", k + 1);
        }
        printf("\n");
    }
    return 0;
}
C
// C Program to print hourglass pattern using alphabets
#include <stdio.h>
int main()
{
    int rows = 5;

    // first outer loop to iterate each row
    for (int i = 0; i < 2 * rows - 1; i++) {

        // assigning comparator
        int comp;
        if (i < rows) {
            comp = 2 * i + 1;
        }
        else {
            comp = 2 * (2 * rows - i) - 3;
        }

        // first inner loop to print leading spaces
        for (int j = 0; j < comp; j++) {
            printf(" ");
        }

        // second inner loop to print alphabets
        for (int k = 0; k < 2 * rows - comp; k++) {
            printf("%c ", k + 'A');
        }
        printf("\n");
    }
    return 0;
}


Output

* * * * * * * * *   |   1 2 3 4 5 6 7 8 9   |   A B C D E F G H I
* * * * * * * | 1 2 3 4 5 6 7 | A B C D E F G
* * * * * | 1 2 3 4 5 | A B C D E
* * * | 1 2 3 | A B C
* | 1 | A
* * * | 1 2 3 | A B C
* * * * * | 1 2 3 4 5 | A B C D E
* * * * * * * | 1 2 3 4 5 6 7 | A B C D E F G
* * * * * * * * * | 1 2 3 4 5 6 7 8 9 | A B C D E F G H I

10. Hollow Square Pattern in C

The Hollow Square Pattern is a square with only the boundary lines. The space inside should be empty in this pattern.

Example

C
// C Program to print hollow square pattern using * star
#include <stdio.h>

int main()
{
    int rows = 5;

    // outer loop to iterator through each row
    for (int i = 0; i < rows; i++) {

        // inner loop to print * star in each row
        for (int j = 0; j < rows; j++) {
            // statement to check boundry condition
            if (i > 0 && i < rows - 1 && j > 0
                && j < rows - 1) {
                printf("  ");
            }
            else {
                printf("* ");
            }
        }
        printf("\n");
    }
    return 0;
}
C
// C Program to print hollow square pattern using numbers
#include <stdio.h>

int main()
{
    int rows = 5;

    // outer loop to iterator through each row
    for (int i = 0; i < rows; i++) {

        // inner loop to print number in each row
        for (int j = 0; j < rows; j++) {
            // statement to check boundry condition
            if (i > 0 && i < rows - 1 && j > 0
                && j < rows - 1) {
                printf("  ");
            }
            else {
                printf("%d ", j + 1);
            }
        }
        printf("\n");
    }
    return 0;
}
C
// C Program to print hollow square pattern using alphabet
#include <stdio.h>

int main()
{
    int rows = 5;

    // outer loop to iterator through each row
    for (int i = 0; i < rows; i++) {

        // inner loop to print alphabet in each row
        for (int j = 0; j < rows; j++) {
            // statement to check boundry condition
            if (i > 0 && i < rows - 1 && j > 0
                && j < rows - 1) {
                printf("  ");
            }
            else {
                printf("%c ", j + 'A');
            }
        }
        printf("\n");
    }
    return 0;
}


Output

* * * * *   |   1 2 3 4 5   |   A B C D E
* * | 1 5 | A E
* * | 1 5 | A E
* * | 1 5 | A E
* * * * * | 1 2 3 4 5 | A B C D E

11. Hollow Full Pyramid Pattern

In the Hollow Pyramid pattern, we only have to print the boundary of the full pyramid.

Example:

C
// C program to print hollow full pyramid using star *
#include <stdio.h>

int main()
{
    int rows = 5;

    // first outer loop to iterate through each loop
    for (int i = 0; i < rows; i++) {

        // first inner loop to print leading whitespaces
        for (int j = 0; j < 2 * (rows - i) - 1; j++) {
            printf(" ");
        }

        // second inner loop to print stars * and inner
        // whitespaces
        for (int k = 0; k < 2 * i + 1; k++) {
            if (k == 0 || k == 2 * i || i == rows - 1) {
                printf("* ");
            }
            else {
                printf("  ");
            }
        }
        printf("\n");
    }
    return 0;
}
C
// C program to print hollow full pyramid using numbers
#include <stdio.h>

int main()
{
    int rows = 5;

    // first outer loop to iterate through each loop
    for (int i = 0; i < rows; i++) {

        // first inner loop to print leading whitespaces
        for (int j = 0; j < 2 * (rows - i) - 1; j++) {
            printf(" ");
        }

        // second inner loop to print numbers and inner
        // whitespaces
        for (int k = 0; k < 2 * i + 1; k++) {
            if (k == 0 || k == 2 * i || i == rows - 1) {
                printf("%d ", k + 1);
            }
            else {
                printf("  ");
            }
        }
        printf("\n");
    }
    return 0;
}
C
// C program to print hollow full pyramid using alphabets
#include <stdio.h>

int main()
{
    int rows = 5;

    // first outer loop to iterate through each loop
    for (int i = 0; i < rows; i++) {

        // first inner loop to print leading whitespaces
        for (int j = 0; j < 2 * (rows - i) - 1; j++) {
            printf(" ");
        }

        // second inner loop to print alphabets and inner
        // whitespaces
        for (int k = 0; k < 2 * i + 1; k++) {
            if (k == 0 || k == 2 * i || i == rows - 1) {
                printf("%c ", k + 'A');
            }
            else {
                printf("  ");
            }
        }
        printf("\n");
    }
    return 0;
}


Output

        *           |           1           |           A
* * | 1 3 | A C
* * | 1 5 | A E
* * | 1 7 | A G
* * * * * * * * * | 1 2 3 4 5 6 7 8 9 | A B C D E F G H I

12. Hollow Inverted Full Pyramid Pattern

In this pattern, we print the inverted full pyramid with only boundary elements and remove the inside elements to make it hollow.

Example:

C
// C Program to print hollow full pyramid pattern using star
// *
#include <stdio.h>

int main()
{
    int rows = 5;

    // first loop iterating through each row
    for (int i = 0; i < rows; i++) {

        // first inner loop to print leading white space
        for (int j = 0; j < 2 * i + 1; j++) {
            printf(" ");
        }

        // second inner loop to print star* and hollow white
        // space
        for (int k = 0; k < 2 * (rows - i) - 1; k++) {
            if (k == 0 || k == 2 * (rows - i) - 2 || i == 0)
                printf("* ");
            else {
                printf("  ");
            }
        }
        printf("\n");
    }
    return 0;
}
C
// C Program to print hollow full pyramid pattern using
// numbers
#include <stdio.h>

int main()
{
    int rows = 5;

    // first loop iterating through each row
    for (int i = 0; i < rows; i++) {

        // first inner loop to print leading white space
        for (int j = 0; j < 2 * i + 1; j++) {
            printf(" ");
        }

        // second inner loop to print number and hollow
        // white space
        for (int k = 0; k < 2 * (rows - i) - 1; k++) {
            if (k == 0 || k == 2 * (rows - i) - 2 || i == 0)
                printf("%d ", k + 1);
            else {
                printf("  ");
            }
        }
        printf("\n");
    }
    return 0;
}
C
// C Program to print hollow full pyramid pattern using
// alphabets
#include <stdio.h>

int main()
{
    int rows = 5;

    // first loop iterating through each row
    for (int i = 0; i < rows; i++) {

        // first inner loop to print leading white space
        for (int j = 0; j < 2 * i + 1; j++) {
            printf(" ");
        }

        // second inner loop to print alphabets and hollow
        // white space
        for (int k = 0; k < 2 * (rows - i) - 1; k++) {
            if (k == 0 || k == 2 * (rows - i) - 2 || i == 0)
                printf("%c ", k + 'A');
            else {
                printf("  ");
            }
        }
        printf("\n");
    }
    return 0;
}


Output

* * * * * * * * *   |   1 2 3 4 5 6 7 8 9   |   A B C D E F G H I
* * | 1 7 | A G
* * | 1 5 | A E
* * | 1 3 | A C
* | 1 | A

13. Hollow Diamond Pattern

This pattern is also similar to the Diamond Pattern but without the inner elements such that it appears hollow inside.

Example:

C
// C Program to print the hollow diamond pattern using star
// *
#include <stdio.h>
int main()
{
    int n = 5;

    // first outer loop to iterator through each row
    for (int i = 0; i < 2 * n - 1; i++) {
        // assigning values to the comparator according to
        // the row number
        int comp;
        if (i < n) {
            comp = 2 * (n - i) - 1;
        }
        else {
            comp = 2 * (i - n + 1) + 1;
        }

        // first inner loop to print leading whitespaces
        for (int j = 0; j < comp; j++) {
            printf(" ");
        }

        // second inner loop to print star * and inner
        // whitespaces
        for (int k = 0; k < 2 * n - comp; k++) {
            if (k == 0 || k == 2 * n - comp - 1) {
                printf("* ");
            }
            else {
                printf("  ");
            }
        }
        printf("\n");
    }
    return 0;
}
C
// C Program to print the hollow diamond pattern using
// numbers
#include <stdio.h>
int main()
{
    int n = 5;

    // first outer loop to iterator through each row
    for (int i = 0; i < 2 * n - 1; i++) {
        // assigning values to the comparator according to
        // the row number
        int comp;
        if (i < n) {
            comp = 2 * (n - i) - 1;
        }
        else {
            comp = 2 * (i - n + 1) + 1;
        }

        // first inner loop to print leading whitespaces
        for (int j = 0; j < comp; j++) {
            printf(" ");
        }

        // second inner loop to print number and inner
        // whitespaces
        for (int k = 0; k < 2 * n - comp; k++) {
            if (k == 0 || k == 2 * n - comp - 1) {
                printf("%d ", k + 1);
            }
            else {
                printf("  ");
            }
        }
        printf("\n");
    }
    return 0;
}
C
// C Program to print the hollow diamond pattern using
// alphabet
#include <stdio.h>
int main()
{
    int n = 5;

    // first outer loop to iterator through each row
    for (int i = 0; i < 2 * n - 1; i++) {
        // assigning values to the comparator according to
        // the row number
        int comp;
        if (i < n) {
            comp = 2 * (n - i) - 1;
        }
        else {
            comp = 2 * (i - n + 1) + 1;
        }

        // first inner loop to print leading whitespaces
        for (int j = 0; j < comp; j++) {
            printf(" ");
        }

        // second inner loop to print alphabet and inner
        // whitespaces
        for (int k = 0; k < 2 * n - comp; k++) {
            if (k == 0 || k == 2 * n - comp - 1) {
                printf("%c ", k + 'A');
            }
            else {
                printf("  ");
            }
        }
        printf("\n");
    }
    return 0;
}


Output

        *           |           1           |           A
* * | 1 3 | A C
* * | 1 5 | A E
* * | 1 7 | A G
* * | 1 9 | A I
* * | 1 7 | A G
* * | 1 5 | A E
* * | 1 3 | A C
* | 1 | A

14. Hollow Hourglass Pattern in C

The hollow hourglass is the pattern in which only the boundary of the hourglass pattern is visible.

Example:

C
// C Program to print the hourglass pattern using star *
#include <stdio.h>
int main()
{
    int n = 5;

    // first outer loop to iterate through each row
    for (int i = 0; i < 2 * n - 1; i++) {

        // assigning comparator
        int comp;
        if (i < n) {
            comp = 2 * i + 1;
        }
        else {
            comp = 2 * (2 * n - i) - 3;
        }

        // first inner loop to print leading whitespaces
        for (int j = 0; j < comp; j++) {
            printf(" ");
        }

        // second inner loop to print star * and inner
        // whitespaces
        for (int k = 0; k < 2 * n - comp; k++) {
            if (k == 0 || k == 2 * n - comp - 1 || i == 0
                || i == 2 * n - 2) {
                printf("* ");
            }
            else {
                printf("  ");
            }
        }
        printf("\n");
    }
    return 0;
}
C
// C Program to print the hourglass pattern using numbers
#include <stdio.h>
int main()
{
    int n = 5;

    // first outer loop to iterate through each row
    for (int i = 0; i < 2 * n - 1; i++) {

        // assigning comparator
        int comp;
        if (i < n) {
            comp = 2 * i + 1;
        }
        else {
            comp = 2 * (2 * n - i) - 3;
        }

        // first inner loop to print leading whitespaces
        for (int j = 0; j < comp; j++) {
            printf(" ");
        }

        // second inner loop to print number and inner
        // whitespaces
        for (int k = 0; k < 2 * n - comp; k++) {
            if (k == 0 || k == 2 * n - comp - 1 || i == 0
                || i == 2 * n - 2) {
                printf("%d ", k + 1);
            }
            else {
                printf("  ");
            }
        }
        printf("\n");
    }
    return 0;
}
C
// C Program to print the hourglass pattern using alphabets
#include <stdio.h>
int main()
{
    int n = 5;

    // first outer loop to iterate through each row
    for (int i = 0; i < 2 * n - 1; i++) {

        // assigning comparator
        int comp;
        if (i < n) {
            comp = 2 * i + 1;
        }
        else {
            comp = 2 * (2 * n - i) - 3;
        }

        // first inner loop to print leading whitespaces
        for (int j = 0; j < comp; j++) {
            printf(" ");
        }

        // second inner loop to print alphabets and inner
        // whitespaces
        for (int k = 0; k < 2 * n - comp; k++) {
            if (k == 0 || k == 2 * n - comp - 1 || i == 0
                || i == 2 * n - 2) {
                printf("%c ", k + 'A');
            }
            else {
                printf("  ");
            }
        }
        printf("\n");
    }
    return 0;
}


Output

* * * * * * * * *   |   1 2 3 4 5 6 7 8 9    |   A B C D E F G H I
* * | 1 7 | A G
* * | 1 5 | A E
* * | 1 3 | A C
* | 1 | A
* * | 1 3 | A C
* * | 1 5 | A E
* * | 1 7 | A G
* * * * * * * * * | 1 2 3 4 5 6 7 8 9 | A B C D E F G H I

15. Floyd’s Triangle in C

In Floyd’s Triangle pattern, instead of starting the sequence of the numbers from 1 in each row, we print consecutive natural numbers. We can also print this pattern for alphabet sequence.

Example:

C
// C Program to print the Floyd's Triangle
#include <stdio.h>

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

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

        // innter loop to print abphabet in each row
        for (int j = 0; j <= i; j++) {
            printf("%d ", n++);
        }
        printf("\n");
    }
    return 0;
}
C
// C Program to print the Floyd's Triangle of Alphabets
#include <stdio.h>

int main()
{
    int rows = 4;
    char n = 'A';

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

        // innter loop to print abphabet in each row
        for (int j = 0; j <= i; j++) {
            printf("%c ", n++);
        }
        printf("\n");
    }
    return 0;
}


Output

1          |   A
2 3 | B C
4 5 6 | D E F
7 8 9 10 | G H I J

16. Pascal’s Triangle in C

A Pascal’s Triangle is a triangular array of binomial coefficients where the nth row contains the binomial coefficients nC0, nC1, nC2, ……. nCn. The following example demonstrates one of the methods using which we can print Pascal’s Triangle Pattern.

Example:

C
// C program to print the pascal's triangle pattern
#include <stdio.h>

int main()
{
    int rows = 5;

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

        // inner loop 1 for leading white spaces
        for (int j = 0; j < rows - i; j++) {
            printf(" ");
        }

        int C = 1; // coefficient

        // inner loop 2 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 

To know more about Pascal’s Triangle, refer to this article – Pascal’s Triangle



Similar Reads

Common Memory/Pointer Related bug in C Programs
Dereferencing an unknown memory location : C programmers mostly use scanf() function to take input, but sometimes a small mistake can bring a bug or even crash whole program. The syntax for scanf() is scanf("%d", &a);. It might be possible to miss a & and write &a as a so now scanf("%d", a); is dereferencing to an unknown location. Now
6 min read
Output of C programs | Set 63
Prerequisite : Structure padding, integer promotion and sequence points. Q1. Consider the following code: C/C++ Code #include<stdio.h> struct geeks{ int i; char c; } obj; int main() { printf("%ld", sizeof(obj)); } What would be the output of the above code? A. 4 B. 5 C. 8 D. Can't be determined Output: Can't be determined Explanatio
4 min read
Facts and Question related to Style of writing programs in C/C++
Here are some questions related to Style of writing C programs: Question-1: Why i++ executes faster than i + 1 ? Answer-1: The expression i++ requires a single machine instruction such as INR to carry out the increment operation whereas, i + 1 requires more instructions to carry out this operation. Question-2: Is writing if(! strcmp(s1, s2) ) a goo
2 min read
Output of C programs | Set 66 (Accessing Memory Locations)
Q1. Is the output of this code True or False? #include <stdio.h> int main(void) { int b = 20; int* y = &b; char n = 'A'; char* z = &n; y[0] = z[0]; printf((*y == *z) ? "True" : "False"); } A. True B. False C. Program would crash D. Compilation error Answer: A. True Explanation: The binary form of 20 is 10100 which
7 min read
CLI programs in C for playing media and shut down the system
Command Line Interface: CLI is a text-based user interface (UI) used to view and manage computer files.Command-line interfaces are also called command-line user interfaces, the console uses interfaces and characters uses interfaces.In programming, the user gives input during the execution of a program i.e., in the running phase. But there are a lot
6 min read
Programs to print Interesting Patterns
Program to print the following pattern: Examples : Input : 5 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * This program is divided into four parts. C/C++ Code // C++ program to print // the given pattern #include<iostream> using namespace std; void pattern(int n
15+ min read
Output of C programs | Set 30 (Switch Case)
Prerequisite - Switch Case in C/C++ Interesting Problems of Switch statement in C/C++ Program 1 #include <stdio.h> int main() { int num = 2; switch (num + 2) { case 1: printf("Case 1: "); case 2: printf("Case 2: "); case 3: printf("Case 3: "); default: printf("Default: "); } return 0; } Output: Default:
2 min read
C/C++ Tricky Programs
We may come across various tricky programs in our day-to-day life. Maybe in technical interviews, coding tests, or C/C++ classrooms. Here is a list of such programs:- Print text within double quotes (" "). This may seem easy, but beginners may get puzzled while printing text within double quotes. C/C++ Code #include <stdio.h> int main() { pri
6 min read
C File Handling Programs
C Program to list all files and sub-directories in a directory C Program to count number of lines in a file C Program to print contents of file C Program to copy contents of one file to another file C Program to merge contents of two files into a third file C program to delete a file
1 min read
Memory Layout of C Programs
A typical memory representation of a C program consists of the following sections. Text segment (i.e. instructions)Initialized data segment Uninitialized data segment (bss)Heap Stack A typical memory layout of a running process 1. Text Segment: A text segment, also known as a code segment or simply as text, is one of the sections of a program in an
6 min read
C Programs
To learn anything effectively, practicing and solving problems is essential. To help you master C programming, we have compiled over 100 C programming examples across various categories, including basic C programs, Fibonacci series, strings, arrays, base conversions, pattern printing, pointers, and more. These C Examples cover a range of questions,
9 min read
Program to print numeric pattern
Given the value of n i.e, the number of rows, print the following pattern. Examples : Input : n = 4 Output : 1 5 2 8 6 3 10 9 7 4 Input : n = 6 Output : 1 7 2 12 8 3 16 13 9 4 19 17 14 10 5 21 20 18 15 11 6 Approach: The approach is to start printing the pattern from the end of each row. After the completion of the last column of each row, start fr
6 min read
Program to print the Cot Bed Pattern
The given task is to draw the Cot Bed pattern using '$' Cot Bed Pattern: $$$$$$$$$$$$$$$$$$$$$$$$$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $$$$$$$$$$$$$$$$$$$$$$$$$ Below is the code to implement the above problem: Program: C/C++ Code // C++ program to print // the Cot Bed Pattern // header files #include <b
10 min read
Magical Pattern
Given an integer N as input, the task is to print the Magical Pattern as given below: N . . 3 2 1 2 3 . . N . . . . . . . . . . . 3 3 3 3 2 1 2 3 3 3 3 2 2 2 2 2 1 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 1 2 2 2 2 2 3 3 3 3 2 1 2 3 3 3 3 . . . . . . . . . . . N . . 3 2 1 2 3 . . N Examples: Input: 3 Output: 3 2 1 2 3 2 2 1 2 2 1 1 1 1 1 2 2 1 2 2
9 min read
C Program for KMP Algorithm for Pattern Searching[duplicate]
C/C++ Code // C++ program for implementation of KMP pattern searching // algorithm #include <bits/stdc++.h> void computeLPSArray(char* pat, int M, int* lps); // Prints occurrences of txt[] in pat[] void KMPSearch(char* pat, char* txt) { int M = strlen(pat); int N = strlen(txt); // create lps[] that will hold the longest prefix suffix // value
2 min read
C Program for Rabin-Karp Algorithm for Pattern Searching
Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function search(char pat[], char txt[]) that prints all occurrences of pat[] in txt[]. You may assume that n > m. Examples: Input: txt[] = "THIS IS A TEST TEXT" pat[] = "TEST" Output: Pattern found at index 10 Input: txt[] = "AABAACAADAABAABA" pat[] = "AABA" Output: Pattern found at ind
3 min read
Substring Reverse Pattern
Given string str, the task is to print the pattern given in the examples below: Examples: Input: str = "geeks" Output: geeks *kee* **e** The reverse of "geeks" is "skeeg" Replace the first and last characters with '*' i.e. *kee* Replace the second and second last character in the modified string i.e. **e** And so on. Input: str = "first" Output: fi
5 min read
Program to print half Diamond star pattern
Given an integer N, the task is to print half-diamond-star pattern. ************************************ Examples: Input: N = 3 Output: * ** *** ** * Input: N = 6 Output: * ** *** **** ***** ****** ***** **** *** ** * Approach: The idea is to break the pattern into two halves that is upper half and lower half. Then print them separately with the he
4 min read
C Program to Print Continuous Character Pattern
Here, we will see how to print continuous character patterns using a C program. Below are the examples: Input: rows = 5Output: A B C D E F G H I J K L M N O Input: rows = 3Output: A B C D E F There are 2 ways to print continuous character patterns in C: Using for loop.Using while loop. Let's discuss each of these in detail. 1. Using for loop Approa
5 min read
C Program For Printing Half Right Number Pyramid Pattern
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 5Pattern 1: Input: n = 5 Output: 1 2 3 4 5 6 7 8 9 10
6 min read
C Program To Print Floyd Triangle Pattern
Here, we will build a C Program To Print Floyd Triangle Pattern using 2 approaches i.e. Using for loop Using while loop Input: n = 5 Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1. 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 increme
2 min read
C Program To Print Diamond Pattern
Here, we will see how to print a full diamond shape pyramid using the C program. Below are the examples: Input: 6Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Input: 8Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
3 min read
C Program To Print Reverse Floyd's Pattern
Here we will develop a C Program To Print The Reverse Floyd pattern. Given the value of row(number of rows), we need the approach of looping and using Nested Loop to print this pattern. Input: row = 5 Output: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 Using the concepts of the Nested loop The approach is to previously calculate the maximum value which is
2 min read
C Program For Printing Simple Half Right Star Pyramid Pattern
Here, we will develop a C Program To Print Simple Half Right Star Pyramid Pattern using two approaches i.e. Using for loop 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
2 min read
C Program To Print Character Pyramid Pattern
Here, we will build a C Program To Print Character Pyramid Pattern using 2 approaches i.e. Using for loopUsing while loop Input: rows = 5 Output: A B B C C C D D D D E E E E E 1. Using for loopApproach 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
5 min read
C Program To Print Continuous Character Pyramid Pattern
There are 2 ways to print continuous character patterns in C i.e: Using for loopUsing while loop Input: rows = 5 Output: A B C D E F G H I J K L M N O1. Using for loopApproach 1: Using CharacterAssign any character to one variable for the printing pattern. The first for loop is used to iterate the number of rows.The second for loop is used to repea
5 min read
C Program To Print Hollow Diamond Pattern
To print the hollow diamond pattern in C, we will use the following 2 approaches: for Loopwhile Loop Input: n = 5 Output: * * * * * * * * * * * * * * * *Approach 1: Using for loop Example: C/C++ Code // C Program To Print Hollow Diamond // Pattern using for loop #include <stdio.h> int main() { int n = 5, rows, columns; // for loop is used to
3 min read
C Program to Print Cross or X Number Pattern
Write a C program to print X number pattern series using a loop. How to print the X number pattern series using for loop in C program. Logic to Print X using numbers in C programming Input N = 5 Output: The pattern consists of Exactly n*2-1 rows and columns. Hence outer loop run rows with(for i=1;i<=count;i++) and run inner loop as for(j=1;j<
1 min read
C Program for Naive algorithm for Pattern Searching
Write a C program for a given text string with length n and a pattern with length m, the task is to print all occurrences of the pattern in text.Note: You may assume that n > m. Examples: Input: text = “THIS IS A TEST TEXT”, pattern = “TEST”Output: Pattern found at index 10 Input: text = “AABAACAADAABAABA”, pattern = “AABA”Output: Pattern found
2 min read
C Program to Print Pyramid Pattern
Pyramid pattern printing is a very popular logical program and it enhances your logical thinking. In this article, we will learn how to make different types of pyramid patterns in c programming. Prerequisites: C if...else StatementC for LoopC while and do...while LoopC break - continuePyramid PatternA pyramid is a 3 - dimensional shape that has a p
7 min read
Article Tags :
three90RightbarBannerImg