C Pattern Programs
C Pattern Programs
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.
The following example programs for printing patterns in the C programming language.
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 program to print right half pyramid pattern of star
#include <stdio.h>
int main()
{
int rows = 5;
int main()
{
int rows = 5;
Output
* | 1 | A
** | 12 | AB
*** | 123 | ABC
**** | 1234 | ABCD
***** | 12345 | ABCDE
Time Complexity: O(rows*rows) as nested for loop is being run.
Auxiliary Space: O(1) as no extra space has been used.
Example:
// C program to print the full pyramid pattern of stars
#include <stdio.h>
int main()
{
int rows = 5;
* | 1 | A
*** | 123 | ABC
***** | 12345 | ABCDE
******* | 1234567 | ABCDEFG
********* | 123456789 | ABCDEFGHI
Time Complexity: O(rows*rows) as nested for loop is being run.
Auxiliary Space: O(1) as no extra space has been used.
Example:
// C program to print the inverted right half pyramid of
// stars
#include <stdio.h>
int main()
{
int rows = 5;
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.
Example:
// C program to print the inverted left half pyramid pattern
// of stars
#include <stdio.h>
int main()
{
int rows = 5;
return 0;
}
Output
Example:
// C program to print the inverted full pyramid pattern of
// stars
#include <stdio.h>
int main()
{
int rows = 5;
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 Program to print the rhombus pattern using * star
#include <stdio.h>
int main()
{
int rows = 5;
Example:
// C Program to print diamond pattern using star *
#include <stdio.h>
int main()
{
int n = 5;
* | 1 | A
*** | 123 | ABC
***** | 12345 | ABCDE
******* | 1234567 | ABCDEFG
********* | 123456789 | ABCDEFGHI
******* | 1234567 | ABCDEFG
***** | 12345 | ABCDE
*** | 123 | ABC
* | 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 Program to print hourglass pattern using star *
#include <stdio.h>
int main()
{
int rows = 5;
// assigning comparator
int comp;
if (i < rows) {
comp = 2 * i + 1;
}
else {
comp = 2 * (2 * rows - i) - 3;
}
int main()
{
int rows = 5;
int main()
{
int rows = 5;
int main()
{
int rows = 5;
int main()
{
int rows = 4;
int n = 1;
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 program to print the pascal's triangle pattern
#include <stdio.h>
int main()
{
int rows = 5;
int C = 1; // coefficient
Output
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1