Most Asked Pattern Programs in C

Posted in /   /  

Most Asked Pattern Programs in C

Vinay Khatri
Last updated on June 10, 2022

    Pattern programs are most commonly asked during interviews, so here in this article, we will be covering the top pattern programs in C.  Moreover, the logic is universal and can be applied to different programming languages, such as C++, Java, and Python.

    Pattern Programs in C

    The patterns that we will be discussing are:

    1. Solid Rectangular Star Pattern in C
    2. Hollow Rectangular star Pattern in C
    3. Half Pyramid Star Pattern in C
    4. Inverted Half Pyramid Pattern in C
    5. Full Pyramid Star Pattern in C
    6. Hollow Pyramid Star Pattern
    7. Inverted Hollow Half pyramid in C
    8. Full Hollow Inverted Pyramid in C
    9. Half Diamond Star pattern in C
    10. Solid Full Diamond Pattern in C
    11. Right Arrow Star Pattern in C
    12. Left Arrow Star Pattern
    13. Plus Star Pattern
    14. X Pattern in C
    15. The Diagonal Hollow Square Pattern in C
    16. Solid Rhombus Star Pattern in C
    17. Hollow Rhombus Star Pattern in C
    18. Solid Mirrored Rhombus Pattern in C
    19. Hollow Mirrored Rhombus Star Pattern
    20. Pascal’s triangle in C
    21. Floyd’s Triangle in C
    22. Half Pyramid of Alphabets in C
    23. Palindrome Half Pyramid Pattern in Alphabets
    24. 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 height of the 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 height of the 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 = 7 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: 6 Preview

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

    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 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++)  
            {  
                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 an uppercase character you want to print in the last row: 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

    Note: Check out C program to print a triangle of alphabets .

    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

    Conclusion

    Those were some of the most popular pattern programs in C. What are your favorite pattern programs? Let us know in the comments. Buy the course here to hone your skills and master the C language.

    People are also reading:

    Leave a Comment on this Post

    0 Comments