Mostly Asked Pattern Programs in C

By | September 7, 2020
Pattern Programs in C

Pattern program are most commonly asked interview questions, so here in this article, we will be covering the top Pattern program in C.  The logic is universal and can be applied in different programming languages such as C++, Java, and Python.

Pattern Programs in C

The Patterns we will be discussing are:

Vamware
  • Solid Rectangular Star Pattern in C
  • Hollow Rectangular star Pattern in C
  •  Half Pyramid Star Pattern in C
  • Inverted half Pyramid Pattern in C
  • Full Pyramid Star Pattern in C
  • Hollow Pyramid Star Pattern
  • Inverted hollow half pyramid in C
  • Full hollow Inverted Pyramid in C
  • Half Diamond Star pattern in C
  • Solid Full Diamond Pattern in C
  • Right Arrow Star Pattern in C
  • Left Arrow Star Pattern
  • Plus Star Pattern
  • X pattern in C
  • The diagonal hollow square pattern in C
  •  Solid Rombus star pattern in C
  • Hollow Rhombus Star Pattern in C
  • Solid Mirrored Rhombus Pattern in C
  • Hollow Mirrored Rhombus Star Pattern
  • Pascal’s triangle in C
  • Floyd’s Triangle in C
  • Half Pyramid of ALphabets in C
  •  Palindrome Half Pyramid Patterns in Alphabets
  •  Palindrome Half Pyramid pattern using numbers in C

1. Solid Rectangular Star Pattern in C

rows: 3

columns: 5

Preview

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

Program

#include <stdio.h>

int main()
{

int rows, columns,i,j;
printf("Enter the number of rows : ");
scanf("%d", &rows);
printf("Enter the number of columns : ");
scanf("%d", &columns);

printf("\n");

/* print solid rectangle*/

for (i = 1; i <= rows; i++)
{
      for (j = 1; j <= columns; j++)
       {
             printf("*");
       }
      printf("\n");
}
return 0;
}

Output

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

2. Hollow Rectangular star Pattern in C

rows: 3

columns: 5

Preview

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

Program

int main()
{
int rows, columns,i, j;;

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

printf("\n");
/*Logic for holow rectangle*/
for (i = 1; i <= rows; i++)

{
	for (j = 1; j <= columns; j++)
	{	
    	if (i==1 || i==rows || j==1 || j==columns)
			printf("*");
		else
			printf(" ");
	}
	printf("\n");
}
return 0;
}

Output

Enter the number of rows : 4
Enter the number of columns : 5

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

3. Half Pyramid Star Pattern in C

stars: 6:

Preview

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

Program

#include 

int main()
{
int stars,i, j;;

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

/*Logic for half Pyramid*/
for (i = 1; i <= stars; i++)

{
	for (j = 1; j <= i; j++)
	{	
    	    printf("*");
	}
	printf("\n");
}

return 0;
}

Output

Enter the number of stars : 6
*
**
***
****
*****
******

4. Inverted half Pyramid Pattern in C

stars: 6

Preview

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

Program

int main()
{

int stars,i, j;;

printf("Enter the number of stars : ");

scanf("%d", &stars);


/*Logic for inverted Pyramid*/
for (i = stars; i >= 1; i--)

{
	for (j = 1 ; j <= i; j++)
	{	
         	printf("*");
	}
	printf("\n");
}

return 0;
}

Output

Enter the number of stars : 6
******
*****
****
***
**
*

5. Full Pyramid Star Pattern in C

height=5

Preview

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

Program

#include <stdio.h>   
int main()  
{  
    int height,space,i,j,k;  
    printf("Enter the hight triangle: ");  
    scanf("%d",&height);  
    space=height;  
   for( i=1;i<=height;i++)  
   {  
       for( j=1;j<space-1;j++)  
       {  
           printf(" ");  
       }  
       for( k=1;k<=2*i-1;k++)  
       {  
         printf("*");  
       }  
       space--;  
     
      printf("\n");  
    }  
    return 0;  
}

Output

Enter the hight triangle: 5
   *
  ***
 *****
*******
*********

6. Hollow Pyramid Star Pattern

Height of Pyramid=:6

Preview

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

Program

#include<stdio.h>
  
int main()  
{  
    int height,space,i,j,k;  
    printf("Height of Pyramid: ");  
    scanf("%d",&height);  
    space=height;  
   for( i=1;i<=height;i++)  
   {  
       for( j=1;j<=space-1;j++)  
       {  
           printf(" ");  
       }  
       for( k=1;k<=2*i-1;k++)  
       {  
          if(k==1 || k==2*i-1 || i==height)  
         printf("*");  
         else  
         printf(" ");  
       }  
       space--;  
     
      printf("\n");  
    }  
    return 0;  
}

Output

Height of Pyramid: 6
     *
    * *
   *   *
  *     *
 *       *
***********

7. Inverted hollow half pyramid in C

height = 5

Preview

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

Program

#include<stdio.h>   
  
int main()  
{  
    int height,i,j;  
    printf("Enter the height of Pyramid: ");  
    scanf("%d",&height);  
    for(i=height;i>=1;i--)  
    {  
      for( j=1;j<=i;j++)  
      {  
         if(j==1 || j==i || i==height)  
          printf("*");  
          else  
          printf(" ");  
      }  
      printf("\n");  
    }  
    return 0;  
}

Output

Enter the height of Pyramid: 7
*******
*    *
*   *
*  *
* *
**
*

8. Full hollow Inverted Pyramid in C

height =5:

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

Program

#include <stdio.h>  
  
int main()  
{  
    int height,space=1,i,j,k;  
    printf("Enter the height of pyramid: ");  
    scanf("%d",&height);  
  
   for( i=height;i>=1;i--)  
   {  
       for( j=1;j<space;j++)  
       {  
           printf(" ");  
       }  
       for( k=1;k<=2*i-1;k++)  
       {  
          if(k==1 || k==2*i-1 || i==height)  
           printf("*");  
           else  
           printf(" ");  
       }  
       space++;  
     
      printf("\n");  
    }  
    return 0;  
}

Output

Enter the height of pyramid: 6
***********
 *       *
  *     *
   *   *
    * *
     *

9. Half Diamond Star pattern in C

max stars: 7

Preview

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

Program

#include<stdio.h>
  
int main()  
{  
    int max_stars,i,j;  
    printf("Enter the width of diamond: ");  
    scanf("%d",&max_stars);  
for( i=1;i<=max_stars;i++)  
{  
  for( j=1;j<=i;j++) { printf("*"); } printf("\n"); } for( i=max_stars-1;i>=1;i--)  
 {  
   for( j=1;j<=i;j++)  
   {  
     printf("*");  
   }  
   printf("\n");  
 }    
     
    return 0;  
}

Output

Enter the width of diamond: 7
*
**
***
****
*****
******
*******
******
*****
****
***
**
*

10. Solid Full Diamond Pattern in C

width: 7

Preview

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

Program

#include<stdio.h>  
int main(void) {  
  int width, space, stars, i, j, k;  
  printf("Enter the width of diamonds: ");  
  scanf("%d",&width);  
  space=width-1;  
   stars=1;  
  for( i=1;i<=width;i++)  
  {  
    for( j=1;j<=space;j++)  
    {  
      printf(" ");  
    }  
    for( k=1;k<=stars;k++) { printf("*"); } if(space>i)  
    {  
      space=space-1;  
      stars=stars+2;  
    }  
    if(space<i)  
    {  
      space=space+1;  
      stars=stars-2;  
    }  
    printf("\n");  
  }  
  return 0;}

Output

Enter the width of diamonds: 7
      *
     ***
    *****
   *******
    *****
     ***
      *

11. Right Arrow Star Pattern in C

width: 7

Preview

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

Program

#include<stdio.h>  
  
int main(void) {  
    
  int width,i,j,k;  
  printf("Enter the width of arrow: ");  
  scanf("%d",&width);  
  
//  arrow upper part
 for( i=0;i<width;i++)  
 {  
   for( j=0;j<i;j++)  
   {  
       printf(" ");  
   }  
   for( k=1;k<=width-i;k++)  
   {  
     printf("*");  
   }  
   printf("\n");  
 }  
 
// arrow lower part
for( i=1;i<width;i++)  
{  
  for( j=1;j<width-i;j++)  
  {  
    printf(" ");  
  }  
  for( k=1;k<=i+1;k++)  
  {  
    printf("*");  
  }  
  printf("\n");  
}  
  return 0;  
}

Output

Enter the width of arrow: 7
*******
******
*****
****
***
**
*
**
***
****
*****
******
*******

12. Left Arrow Star Pattern

width : 6

Preview

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

Program

#include<stdio.h> 
  
int main(void) {  
    
  int width,i,j,k;  
  printf("Enter the width of arrow: ");  
  scanf("%d",&width);  
 for( i=1;i<=width;i++)  
 {  
   for( j=1;j<=width-i;j++)  
   {  
       printf(" ");  
   }  
   for( k=0;k<=width-i;k++)  
   {  
     printf("*");  
   }  
   printf("\n");  
 }  
for( i=1;i<width;i++)  
{  
  for( j=1;j<i+1;j++)  
  {  
    printf(" ");  
  }  
  for( k=1;k<=i+1;k++)  
  {  
    printf("*");  
  }  
  printf("\n");  
}  
  return 0;  
}

Output

Enter the width of arrow: 6
     ******
    *****
   ****
  ***
 **
*
 **
  ***
   ****
    *****
     ******

13. Plus Star Pattern

height: 7

Preview

   +
   +
   +
+++++++
   +
   +
   +

Program

#include<stdio.h>  
  
int main(void) {  
  int height,i,j;  
  printf("Enter the height (odd): ");  
  scanf("%d", &height);  
  for( i=1;i<=height;i++)  
  {  
    if(i==((height/2)+1))  
    {  
      for( j=1;j<=height;j++)  
      {  
        printf("+");  
      }  
   
    }  
    else  
    {  
    for( j=1;j<=height/2;j++)  
    {  
      printf(" ");  
    }  
    printf("+");  
    }  
    printf("\n");  
  }  
  return 0;  
}

Output

Enter the height (odd): 7
   +
   +
   +
+++++++
   +
   +
   +

14. X pattern in C

width: 6

Preview

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

Program

#include<stdio.h> 
  
int main(void) {  
  int width,space, i,j;  
  printf("Enter the width: ");  
  scanf("%d",&width);  
  space=2*width-1;  
  for( i=1;i<=space;i++)  
  {  
    for( j=1;j<=space;j++)  
    {  
       if(i==j || j==(space-i+1))  
       {  
         printf("*");  
       }  
       else  
       {  
         printf(" ");  
       }  
    }  
    printf("\n");  
  }  
  return 0;  
}

Output

Enter the width: 6
*         *
 *       *
  *     *
   *   *
    * *
     *
    * *
   *   *
  *     *
 *       *
*         *

15. The diagonal hollow square pattern in C

rows: 8

Preview

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

Program

#include<stdio.h> 
int main()  
{  
    int rows, i, j;  
    printf("Enter rows: ");  
    scanf("%d",&rows);  
    for( i=1;i<=rows;i++)  
    {  
        for( j=1;j<=rows;j++)  
        {  
            if(i==1 ||i==rows||j==1||j==rows-i+1||i==j||j==rows)  
            {  
            printf("*");  
            }  
            else  
            {  
                     printf(" ");  
                  }                  
            }        
        printf("\n");  
    }  
      
    return 0;  
}

Output

Enter rows: 8
********
**    **
* *  * *
*  **  *
*  **  *
* *  * *
**    **
********

16. Solid Rombus star pattern in C

rows: 7

Preview

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

Program

#include<stdio.h>
  
int main()  
{  
    int rows, i,j,k;  
    printf("Enter rows: ");  
    scanf("%d",&rows);  
    for( i=rows;i>=1;i--)  
    {  
        for( j=1;j<=i-1;j++)  
        {  
            printf(" ");  
        }  
        for( k=1;k<=rows;k++)  
        {  
            printf("*");  
        }  
        printf("\n");  
    }  
    return 0;  
}

Output

Enter rows: 7
      *******
     *******
    *******
   *******
  *******
 *******
*******

17. Hollow Rhombus Star Pattern in C

rows: 7

Preview

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

Program

#include<stdio.h>
  
int main()  
{  
    int rows,i,j,k;  
    printf("Enter rows: ");  
    scanf("%d",&rows);  
    for( i=rows;i>=1;i--)  
    {  
        for( j=1;j<=i-1;j++)  
        {  
            printf(" ");  
        }  
        for( k=1;k<=rows;k++)  
        {  
           if(i==1 || i==rows || k==1 || k==rows)  
            printf("*");  
            else  
            printf(" ");   
        }  
        printf("\n");  
    }  
    return 0;  
}

Output

Enter rows: 7
      *******
     *     *
    *     *
   *     *
  *     *
 *     *
*******

 

18. Solid Mirrored Rhombus Pattern in C

rows: 7

Preview

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

Program

#include<stdio.h>   
int main()  
{  
    int rows,i,j,k;  
    printf("Enter rows: ");  
    scanf("%d",&rows);  
    for( i=1;i<=rows;i++)  
    {  
        for( j=1;j<i;j++)  
        {  
            printf(" ");  
        }  
        for( k=1;k<=rows;k++)  
        {  
           printf("*");  
              
        }  
        printf("\n");  
    }  
    return 0;  
}

Output

Enter rows: 7
*******
 *******
  *******
   *******
    *******
     *******
      *******

19. Hollow Mirrored Rhombus Star Pattern

rows: 7

Preview

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

Program

#include   
int main()  
{  
    int rows, i,j,k;  
    printf("Enter rows: ");  
    scanf("%d",&rows);  
    for( i=1;i<=rows;i++)  
    {  
        for( j=1;j<i;j++)  
        {  
            printf(" ");  
        }  
        for( k=1;k<=rows;k++)  
        {  
         if(i==1 || i==rows || k==1 || k==rows)  
           printf("*");  
            else  
            printf(" ");  
        }  
        printf("\n");  
    }  
    return 0;  
}

Output

Enter rows: 7
*******
 *     *
  *     *
   *     *
    *     *
     *     *
      *******

20. Pascal’s triangle in C

rows:6

Preview

          1
        2 3 2
      3 4 5 4 3
    4 5 6 7 6 5 4
  5 6 7 8 9 8 7 6 5
6 7 8 9 10 11 10 9 8 7 6

Program

#include <stdio.h>
int main() {
   int i, s, r, k = 0, c1 = 0, c2 = 0;
   printf("Enter rows(r): ");
   scanf("%d", &r);
   for (i = 1; i <= r; ++i) {
      for (s = 1; s <= r - i; ++s) {
         printf("  ");
         ++c1;
      }
      while (k != 2 * i - 1) {
         if (c1 <= r - 1) {
            printf("%d ", i + k);
            ++c1;
         } else {
            ++c2;
            printf("%d ", (i + k - 2 * c2));
         }
         ++k;
      }
      c2 = c1 = k = 0;
      printf("\n");
   }
   return 0;
}

Output

Enter rows(r): 6
          1
        2 3 2
      3 4 5 4 3
    4 5 6 7 6 5 4
  5 6 7 8 9 8 7 6 5
6 7 8 9 10 11 10 9 8 7 6

21. Floyd’s Triangle in C

rows: 6

Preview

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21

Program

#include<stdio.h> 
int main() {
   int rows, i, j, n = 1;
   printf("Enter rows: ");
   scanf("%d", &rows);
   for (i = 1; i <= rows; i++) {
      for (j = 1; j <= i; ++j) {
         printf("%d ", n);
         ++n;
      }
      printf("\n");
   }
   return 0;
}

Output

Enter rows: 6
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21

22. Half Pyramid of ALphabets in C

Last Alphabet: F

Preview

A
B B
C C C
D D D D
E E E E E
F F F F F F

Program

#include<stdio.h> 
int main() {
   int i, j;
   char input, alphabet = 'A';
   printf("Enter an uppercase character you want to print in the last row: ");
   scanf("%c", &input);
   for (i = 1; i <= (input - 'A' + 1); ++i) {
      for (j = 1; j <= i; ++j) {
         printf("%c ", alphabet);
      }
      ++alphabet;
      printf("\n");
   }
   return 0;
}

Output

Enter Last ALphabet in uppercase: F
A
B B
C C C
D D D D
E E E E E
F F F F F F

23. Palindrome Half Pyramid Patterns in Alphabets

rows: 5

Preview

A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDEFEDCBA

Program

#include
  
int main() {
   int i, j, rows, c=0;
    
   printf("Enter rows: ");
   scanf("%d", &rows);
     
   for (i = 1; i <= 2*rows; i=i+2) {
      for (j = 1; j <= i; j++) {
       printf("%c", 'A'+c);
       if(j <= i/2)
           c++;
        else 
           c--;
      }
      c = 0;
      printf("\n");
   }
   return(0);
}

Output

Enter rows: 6
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDEFEDCBA

24. Palindrome Half Pyramid pattern using numbers in C

rows: 6

Preview

1
121
12321
1234321
123454321
12345654321

Program

#include
  
int main() {
   int i, j,k, rows, c=0;
    
   printf("Enter rows: ");
   scanf("%d", &rows);
     
   for(i=1; i<=rows; i++)
   {
   	for(j=1;j<=i;j++) { printf("%d",j); } for(k=i-1;k>=1;k--)
	{
		printf("%d",k);
	}
	printf("\n");
   }
   return(0);
}

Output

Enter rows: 6
1
121
12321
1234321
123454321
12345654321


											

Leave a Reply

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