Pattern Programs in C

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest in 3 Numbers
Find Grade of student
Find the absolute value
Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

Pattern programs in C | Here we will write patterns programs in the C language. Some patterns and test cases are given, after observing those patterns we will write C programs to display them on the screen. To display patterns we need nested loops. Loops can be while or for loop, but writing programs using for loop is easy compared to the while loop. Prerequisite to solve these programs:- While loop in C, Do-while loop in C, While vs do-while, and For loop in C

Note:- Outer loop is used for rows and the inner loop is used to display columns (assume Matrix). That’s why the number of rows is controlled by the outer loop but for displaying values in those columns inner loop is used.

In these programs for a better understanding purpose, we have used r, c as loop iterator variables (r => row, c => column). But generally, we use i, j, k, and e.t.c. as the loop iterator variables.

Table of Contents

  1. C Program For the Given Pattern
  2. Pattern Programs in C for Half Pyramid
  3. Pattern Programs in C for Floyd’s Triangle
  4. Some Similar Pattern Programs in C
  5. Pattern Programs in C for Inverted Half Pyramid
  6. Pattern Programs in C for Full Pyramid
  7. Pattern Programs in C for Inverted Full Pyramid
  8. More Pattern programs in C

C Program For the Given Pattern

Pattern Program 1

In the below pattern each column contains N number of stars where N is the number of rows. The number of rows and columns are the same so we can assume it as a square matrix.

Sample Input/ Output:-

Enter the number of rows: 3
* * *
* * *
* * *

Enter the number of rows: 5
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *

#include<stdio.h>
int main()
{
   int n;

   printf("Enter the number of rows: ");
   scanf("%d",&n);

   for(int r=1; r<=n; r++)
   {
     for(int c=1; c<=n; c++)
     {
       printf("* ");
     }
     printf("\n");
   }

   return 0;
}

Pattern Program 2

In the below pattern columns are started from 1 to N, and the number of columns is equal to the number of rows.

Sample Input/ Output:-

Enter the number of rows: 3
1 2 3
1 2 3
1 2 3

Enter number of rows: 5
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

#include<stdio.h>
int main()
{
   int n;

   printf("Enter the number of rows: ");
   scanf("%d",&n);

   for(int r=1; r<=n; r++)
   {
     for(int c=1; c<=n; c++)
     {
       printf("%3d",c);
     }
     printf("\n");
   }

   return 0;
}

Pattern Program 3

The below pattern program is similar to the previous one but in this pattern, each Nth row contains the same N value. It is also a square matrix where the number of rows is equal to the number of columns.

Sample Input/ Output:-

Enter the number of rows: 3
1 1 1
2 2 2
3 3 3

Enter number of rows: 5
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5

#include<stdio.h>
int main()
{
   int n;

   printf("Enter the number of rows: ");
   scanf("%d",&n);

   for(int r=1; r<=n; r++)
   {
     for(int c=1; c<=n; c++)
     {
       printf("%3d",r);
     }
     printf("\n");
   }

   return 0;
}

Pattern Program 4

Sample Input/ Output:-

Enter the number of rows: 3
a a a
b b b
c c c

Enter the number of rows: 5
a a a a a
b b b b b
c c c c c
d d d d d
e e e e e

#include<stdio.h>
int main()
{
   int n;

   printf("Enter number of rows: ");
   scanf("%d",&n);

   for(int r=1; r<=n; r++)
   {
     for(int c=1; c<=n; c++)
     {
       printf("%3c",r+96);
     }
     printf("\n");
   }

   return 0;
}

Pattern Program 5

The below pattern is similar to the previous pattern program but instead of the lowercase letter, it has all uppercase letters in the pattern. The ASCII value of the character ‘A’ is 65.

Sample Input/ Output:-

Enter the number of rows: 3
A A A
B B B
C C C

Enter the number of rows: 5
A A A A A
B B B B B
C C C C C
D D D D D
E E E E E

#include<stdio.h>
int main()
{
   int n;

   printf("Enter number of rows: ");
   scanf("%d",&n);

   for(int r=1; r<=n; r++)
   {
     for(int c=1; c<=n; c++)
     {
       printf("%3c",r+64);
     }
     printf("\n");
   }

   return 0;
}

Pattern Program 6

In the previous pattern (i.e. pattern program 5) one row was containing similar characters.

But in the current pattern one row contain characters starting from ‘A’ to the number of rows and every row is similar.

Sample Input/ Output:-

Enter the number of rows: 3
A B C
A B C
A B C

Enter the number of rows: 5
A B C D E
A B C D E
A B C D E
A B C D E
A B C D E

#include<stdio.h>
int main()
{
   int n;

   printf("Enter the number of rows: ");
   scanf("%d", &n);

   for(int r=1; r<=n; r++)
   {
     for(int c=1; c<=n; c++)
     {
       printf("%3c",c+64);
     }
     printf("\n");
   }

   return 0;
}

Pattern Program 7

Sample Input/ Output:-

Enter the number of rows: 3
a b c
a b c
a b c

Enter the number of rows: 5
a b c d e
a b c d e
a b c d e
a b c d e
a b c d e

#include<stdio.h>
int main()
{
   int n;

   printf("Enter the number of rows: ");
   scanf("%d",&n);

   for(int r=1; r<=n; r++)
   {
     for(int c=1; c<=n; c++)
     {
       printf("%3c",c+96);
     }
     printf("\n");
   }

   return 0;
}

Pattern Program 8

The above pattern contains both uppercase and lowercase characters but in alternate rows. And one row contains a similar character.

In these patterns, if the row number is odd then the small letter alphabet is printed. If the row number is even then the capital letter alphabet is printed.

Sample Input/ Output:-

Enter the number of rows: 3
a a a
B B B
c c c

Enter the number of rows: 9
a a a a a a a a a
B B B B B B B B B
c c c c c c c c c
D D D D D D D D D
e e e e e e e e e
F F F F F F F F F
g g g g g g g g g
H H H H H H H H H
i i i i i i i i i

#include<stdio.h>
int main()
{
   int n;

   printf("Enter the number of rows: ");
   scanf("%d", &n);

   for(int r=1; r<=n; r++)
   {
     for(int c=1; c<=n; c++)
     {
       if(r%2==0) printf("%3c", r+64);
       else printf("%3c", r+96);
     }
     printf("\n");
   }

   return 0;
}

Pattern Program 9

Sample input/outputs:-

Enter the number of rows: 5
1 1 1 1 1
1 1 1 2 2
1 1 3 3 3
1 4 4 4 4
5 5 5 5 5

Enter the number of rows: 10
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 2 2
1 1 1 1 1 1 1 3 3 3
1 1 1 1 1 1 4 4 4 4
1 1 1 1 1 5 5 5 5 5
1 1 1 1 6 6 6 6 6 6
1 1 1 7 7 7 7 7 7 7
1 1 8 8 8 8 8 8 8 8
1 9 9 9 9 9 9 9 9 9
10 10 10 10 10 10 10 10 10 10

#include<stdio.h>
int main()
{
  int n,r,c;

  printf("Enter number of rows: ");
  scanf("%d",&n);

  // outer loop
  for(r=1; r<=n; r++)
  {
    // inner loop
    for(c=1; c<=n; c++)
    {
      if(c <= n-r) printf("%4d",1);
      else printf("%4d",r);
    } // end of inner loop

    printf("\n");
  } // end of outer loop

  return 0;
}

Pattern Program 10

Sample input/outputs:-

Enter the number of rows: 5
* * * * *
* A B C *
* D E F *
* G H I *
* * * * *

Enter the number of rows: 10
* * * * * * * * * *
* A B C D E F G H *
* I J K L M N O P *
* Q R S T U V W X *
* Y Z A B C D E F *
* G H I J K L M N *
* O P Q R S T U V *
* W X Y Z A B C D *
* E F G H I J K L *
* * * * * * * * * *

In the given above pattern, each row starts and ends with a star symbol, and the first and last rows contain only a star symbol whereas the remaining columns contain the alphabet.

In this pattern, when row number and column number are first and last then there are stars only. Otherwise, there are alphabets from ‘A’ to ‘Z’. When the alphabet is reached at ‘Z’ then it again started from ‘A’.

#include<stdio.h>
int main()
{
  int n,r,c;
  char ch = 'A';

  printf("Enter number of rows: ");
  scanf("%d",&n);

  // outer loop
  for(r=1; r<=n; r++)
  {
    // inner loop
    for(c=1; c<=n; c++)
    {
      if(c==1||r==1||c==n||r==n) printf("* ");
      else printf("%c ",ch++);
      if(ch > 'Z') ch='A';
    } // end of inner loop

    printf("\n");
  } // end of outer loop

  return 0;
}

Pattern Programs in C for Half Pyramid

Half Pyramid using * and Increment Operator

Pattern Program 11

Sample Output:-

*
* *
* * *
* * * *
* * * * *

#include<stdio.h>
int main()
{
  int i,j;

  // outer for loop to represent rows
  for(i=1;i<=5;i++)
  {
    // inner for loop to represent columns
    for(j=1;j<=i;j++)
    {
      printf("* ");
    }

    // new line
    printf("\n");
  }

  return 0;
}

Half Pyramid using *, Increment & Decrement Operators

Here we will use the decrement operator for the outer loop and the increment operator for the inner loop to print the above print.

Pattern Program 12

#include<stdio.h>
int main()
{
  int i,j;

  // outer for loop with decrement operator
  for(i=5;i>=1;i--)
  {

    // inner for loop with increment operator
    for(j=i;j<=5;j++)
    {
      printf("* ");
    }

    // new line
    printf("\n");
  }

  return 0;
}

Now, we will use the increment operator for the outer loop and the decrement operator for the inner loop to print the same (above) pattern.

#include<stdio.h>
int main()
{
  int i,j;

  // outer for loop with increment operator 
  for(i=1;i<=5;i++)
  {

     // inner for loop with decrement operator
     for(j=i;j>=1;j--)
     {
       printf("* ");
     }

     printf("\n");
  }
  return 0;
}

Half Pyramid using Numbers

Pattern Program 13

Sample Output:-

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

#include<stdio.h>
int main()
{
   int i,j;

   for(i=1;i<=5;i++)
   {

     for(j=i;j>=1;j--)
     {
       printf("%d ",j);
     }

     printf("\n");
   }

   return 0;
}

Pattern Programs in C for Floyd’s Triangle

Pattern Program 14

Sample input/outputs:-

Enter number of rows: 5
     1
     2    3
     4    5    6
     7    8    9   10
    11   12   13   14   15
Enter number of rows: 10
   1
   2    3
   4    5    6
   7    8    9   10
  11   12   13   14   15
  16   17   18   19   20   21
  22   23   24   25   26   27   28
  29   30   31   32   33   34   35   36
  37   38   39   40   41   42   43   44   45
  46   47   48   49   50   51   52   53   54   55

C Program for the Floyd’s Triangle Pattern,

#include<stdio.h>
int main()
{
   int n, r, c, a=1;

   printf("Enter number of rows: ");
   scanf("%d", &n);

   for(r=1; r<=n; r++)
   {
     for(c=1; c<=r; c++) 
     printf("%5d",a++);

     printf("\n");
   }

   return 0;
}

Some Similar Pattern Programs in C

Pattern Program 15

Sample input/outputs:-

Enter number of rows: 5
     1
     3    2
     4    5    6
    10    9    8    7
    11   12   13   14   15
Enter number of rows: 10
     1
     3    2
     4    5    6
    10    9    8    7
    11   12   13   14   15
    21   20   19   18   17   16
    22   23   24   25   26   27   28
    36   35   34   33   32   31   30   29
    37   38   39   40   41   42   43   44   45
    55   54   53   52   51   50   49   48   47   46

In this pattern, when the row is odd then digits are printed from left to right and when the row is even then digits are printed from right to left.

#include<stdio.h>
int main()
{
   int n, r, c;
   int a=1, b;

   printf("Enter number of rows: ");
   scanf("%d",&n);

   for(r=1; r<=n; r++)
   {
     b=a+r-1;
     for(c=1; c<=r; c++, a++)
     {
       if(r%2==1) printf("%5d",a);
       else printf("%5d",b--);
     }

     printf("\n");
   }

   return 0;
}

Pattern Program 16

Sample input/outputs:-

Enter the number of rows: 5
*
$ *
* $ *
$ * $ *
* $ * $ *

Enter the number of rows: 10
*
$ *
* $ *
$ * $ *
* $ * $ *
$ * $ * $ *
* $ * $ * $ *
$ * $ * $ * $ *
* $ * $ * $ * $ *
$ * $ * $ * $ * $ *

In this pattern, if row number + column number is odd then there is a star (*) symbol at that place else there is a dollar ($) symbol.

#include<stdio.h>
int main()
{
  int n, r, c;

  printf("Enter number of rows: ");
  scanf("%d",&n);

  for(r=1; r<=n; r++)
  {
    for(c=1; c<=r; c++)
    {
      if((r+c)%2==0) printf("* ");
      else printf("$ ");
    }

    printf("\n");
  }

  return 0;
}

Pattern Program 17

Sample input/outputs:-

Enter the number of rows: 5
A
1 1
B B B
2 2 2 2
C C C C C

Enter the number of rows: 10
A
1 1
B B B
2 2 2 2
C C C C C
3 3 3 3 3 3
D D D D D D D
4 4 4 4 4 4 4 4
E E E E E E E E E
5 5 5 5 5 5 5 5 5 5

In this pattern, when the row number is odd then the same alphabet is printed in the row otherwise the same digit is printed in that row.

#include<stdio.h>
int main()
{
  int n, r, c, a=0;
  char ch='A';

  printf("Enter number of rows: ");
  scanf("%d",&n);

  for(r=1; r<=n; r++)
  {
    for(c=1; c<=r; c++)
    {
      if(r%2==1) printf("%5c",ch);
      else printf("%5d",a);
    }

    printf("\n");
    if(r%2==0) ch++;
    else a++;
  }

  return 0;
}

Pattern Program 18

Sample Input/Output:-

Enter number of rows: 3
*
**
***
Enter number of rows: 5
    *
   **
  ***
 ****
*****

C Program for the above pattern,

#include<stdio.h>
int main()
{
  int n, r, c, s;

  printf("Enter number of rows: ");
  scanf("%d",&n);

  for(r=1;r<=n;r++)
  {
    for(s=1;s<=n-r;s++) printf(" ");
    for(c=1;c<=r;c++) printf("*");

    printf("\n");
  }

  return 0;
}

Pattern Program 19

Sample Input/Output:-

Enter number of rows: 3
*
* *
* * *
Enter number of rows: 5
    *
   * *
  * * *
 * * * *
* * * * *

C Program to print pyramid of stars,

#include<stdio.h>
int main()
{
    int n, r, c, s;

    printf("Enter number of rows: ");
    scanf("%d",&n);

    for(r=1;r<=n;r++)
    {
      for(s=1;s<=n-r;s++) printf(" ");
      for(c=1;c<=r;c++) printf("* ");

      printf("\n");
    }

    return 0;
}

The difference between the previous program and this program is only one space. There is an extra space in the line for(c=1;c<=r;c++) printf("* "); in comparison to the previous program.


Pattern Programs in C for Inverted Half Pyramid

Half Pyramid using * and Decrement Operators

Pattern Program 20

Sample Output:-

* * * * *
* * * *
* * *
* *
*

#include<stdio.h>
int main()
{
  int i,j;

  for(i=5;i>=1;i--)
  {

    for(j=i;j>=1;j--)
    {
       printf("* ");
    }

    printf("\n");
  }

  return 0;
}

Some Similar Pattern Programs in C

Pattern program 21:- Display Given Pattern

Print below pattern using increment operators only. This pattern is a combination of the previous two patterns, where the sign is changed from * to -.

- - - - *
- - - * *
- - * * *
- * * * *
* * * * *

#include<stdio.h>
int main()
{
  int i,j,k;

  for(i=1;i<=5;i++)
  {

    for(j=i;j<5;j++)
    {
      printf("- ");
    }

    for(k=1;k<=i;k++)
    {
      printf("* ");
    }

    printf("\n");
  }

  return 0;
}

Pattern Programs in C for Full Pyramid

Pattern program 22: Display Full Pyramid

Sample Input/Output:-

Enter number of rows: 5
*
***
*****
*******
*********
 Enter number of rows: 7
      *
     ***
    *****
   *******
  *********
 ***********
*************

C program for the above full pyramid star pattern,

#include<stdio.h>
int main()
{
    int n, r, c, s;

    printf("Enter number of rows: ");
    scanf("%d",&n);

    for(r=1;r<=n;r++)
    {
      for(s=1;s<=n-r;s++) printf(" ");
      for(c=1;c<=(2*r-1);c++) printf("*");

      printf("\n");
    }

    return 0;
}

Full Pyramid on the Center of the Screen

Pattern program 23

Problem:- Display full pyramid on the center of the computer screen.

Enter number of rows: 7
                                    *
                                   ***
                                  *****
                                 *******
                                *********
                               ***********
                              *************

Generally, on a computer screen, we can print a Maximum of 80 characters horizontally. Here we will print a full pyramid for N lines.

#include<stdio.h>
int main()
{
   int n,i,j,k,c=80;

   printf("Enter number of rows: ");
   scanf("%d", &n);

   for(i=1;i<=n;i++)
   {
     for(j=1;j<=(c/2-i);j++)
     {
       printf(" "); // blank space
     }

     for(k=1;k<=(2*i-1);k++)
     {
       printf("*");
     }

     printf("\n");
   }

   return 0;
}

Full Pyramid with Numbers

Pattern program 24

Sample Input/Output:-

Enter number of rows: 5
1
121
12321
1234321
123454321
Enter number of rows: 7
       1
      121
     12321
    1234321
   123454321
  12345654321
 1234567654321

C Program for the above full pyramid pattern with numbers

#include<stdio.h>
int main()
{
   int n, r, c, k, a;

   printf("Enter number of rows: ");
   scanf("%d",&n);

   for(r=1;r<=n;r++)
   {
     for(c=1;c<=n-r;c++) printf(" ");

     for(k=1;k<=(2*r-1);k++)
     {
       if(k<r) printf("%d",k);
       else if(k==r)
       {
         printf("%d",k);
         a=k;
       }
       else printf("%d",--a);
     }

     printf("\n");
   }

   return 0;
}

In this program, when column number (except space) is less than row number then digits are continuously increasing, and after that digits are decreasing.


Pattern Programs in C for Inverted Full Pyramid

Pattern program 25

Sample Input/Output:-

Enter number of rows: 5
* * * * *
* * * *
* * *
* *
*
Enter number of rows: 7 
* * * * * * *
 * * * * * *
  * * * * *
   * * * *
    * * *
     * *
      *

C program for the inverted full pyramid of stars

#include<stdio.h>
int main()
{
  int n, r, c, s;

  printf("Enter number of rows: ");
  scanf("%d",&n);

  for(r=n;r>=1;r--)
  {
    for(s=1;s<=n-r;s++) printf(" ");
    for(c=1;c<=r;c++) printf("* ");

    printf("\n");
  }

  return 0;
}

More Pattern programs in C

Pattern Program 26

Sample Input/Output:-

Enter number of rows: 5
 *        *
 **      **
 ***    ***
 ****  **** 
 **********
Enter number of rows: 7
 *            *
 **          **
 ***        ***
 ****      ****
 *****    *****
 ******  ******
 **************

C program for the above-given pattern,

#include<stdio.h>
int main()
{
  int n, r, c;

  printf("Enter number of rows: ");
  scanf("%d",&n);

  for(r=1;r<=n;r++)
  {
    for(c=1;c<=2*n;c++)
    if(c<=r||c>(2*n-r)) printf("* ");
    else printf(" ");

    printf("\n");
  }

  return 0;
}

Pattern Program 27

Sample Input/Output:-

Enter number of lines: 5
 ABCDEFGFEDCBA
 ABCDE   EDCBA
 ABCD     DCBA
 ABC       CBA
 AB         BA
 A           A
Enter number of lines: 7
 ABCDEFGHIHGFEDCBA
 ABCDEFG   GFEDCBA
 ABCDEF     FEDCBA
 ABCDE       EDCBA
 ABCD         DCBA
 ABC           CBA
 AB             BA
 A               A

C Program for the above-given pattern

#include<stdio.h>
int main()
{
  int n;
  char ch;

  printf("Enter number of lines: ");
  scanf("%d",&n);

  for(int i=0; i<=n; i++)
  {
    ch = 'A';
    for(int j=0; j<=n-i; j++, ch++)
    {
      printf("%c",ch);
    }

    if(i==0)
    {
      printf("%c",ch);
    }
    else
    {
      for(int k=0; k<(2*i)+1; k++){
        printf(" ");
      }
    }

    ch--;
    for(int j=0; j<=n-i; j++, ch--)
    {
      printf("%c",ch);
    }

    printf("\n");
  }
  return 0;
}

More pattern programs in C,

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest among 3 Numbers
Find Grade of student
Find the absolute value
Check Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

Leave a Comment

Your email address will not be published. Required fields are marked *