C Pattern Programs with Source Code

C Pattern Programs with Source Code

Table of content

Show More

In this blog, you’ll learn the essential concepts of pattern programming and receive hands-on experience designing your patterns, as we are going to cover the basic, intermediate, and advanced C patterns with their source code. 

Explore the world of C Programming with this captivating YouTube video—your gateway to a comprehensive learning experience!

Video Thumbnail

What are C Patterns?

The C pattern typically refers to commonly used practices in the C programming language. These patterns represent established solutions to recurring problems or design approaches that make code more efficient, maintainable, and readable. They consist various aspects of coding, including data structures, algorithms, design paradigms, and optimization techniques. Examples of C patterns include the singleton pattern for ensuring a class has only one instance, the observer pattern for handling communication between objects, and the factory pattern for creating objects without specifying the exact class. These patterns serve as a valuable tool for developers to enhance their code quality and streamline the development process in C.

If you want to know more about C programming, you can go through this C Programming Certification Course!

Basic C Patterns

Basic C patterns often include programs that demonstrate fundamental concepts such as loops, conditionals, and basic data structures. These basic C patterns help beginners grasp foundational programming concepts while exploring various patterns and shapes through simple code structures. 

Square Pattern

Refer to the code below to display a square pattern:

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

Output:

image 105

Right Angled Triangle

Refer to the code below to display a right-angled triangle pattern:

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

Output:

image 106

Inverted Right Angle Triangle

Refer to the code below to display an inverted right-angled triangle pattern:

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

Output:

image 107

Pyramid

Refer to the code below to display a pyramid pattern:

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

Output:

image 108

Alphabetic Pyramid

Refer to the code below to display an alphabetic pyramid pattern:

#include <stdio.h> 
  int main() 

    int rows = 5; 
    for (int i = 0; i < rows; i++) { 
        for (int j = 0; j < 2 * (rows - i) - 1; j++) { 
            printf(" "); 
        } 
        for (int m = 0; m < 2 * i + 1; m++) { 
            printf("%d ", m + 1); 
        } 
        printf("\n"); 
    } 
    return 0; 
}

Output:

image 109

Get 100% Hike!

Master Most in Demand Skills Now!

Intermediate C Patterns

Intermediate C patterns refer to common structures or arrangements of code used in C programming that lie between basic programming constructs and more complex design patterns. These intermediate patterns often involve efficient handling of data structures, conditional statements, loops, and functions to accomplish specific tasks or solve common programming problems. They assist in organizing code, improving readability, and enhancing code reusability. These patterns use techniques for memory management, error handling, algorithm design, or optimizing code performance without necessarily getting into the complexities of full-fledged design patterns typically found in object-oriented programming languages.

Floyd’s Triangle

Below is the code for your reference to print Floyd’s triangle.

#include <stdio.h> 
int main() 

    int rows = 4; 
    int n = 1; 
    // outer loop 
    for (int i = 0; i < rows; i++) { 
        // innter loop 
        for (int j = 0; j <= i; j++) { 
            printf("%d ", n++); 
        } 
        printf("\n"); 
    } 
    return 0; 
}

Output:

image 110

Diamond Pattern

Below is the code for your reference to print a diamond pattern.

#include <stdio.h> 
int main() 

    int n = 5; 
    // first outer loop 
    for (int i = 0; i < 2 * n - 1; i++) { 
        int row; 
        if (i < n) { 
            row = 2 * (n - i) - 1; 
        } 
        else { 
            row = 2 * (i - n + 1) + 1; 
        } 
        // whitespaces 
        for (int j = 0; j < row; j++) { 
            printf(" "); 
        } 
        // stars
        for (int k = 0; k < 2 * n - row; k++) { 
            printf("* "); 
        } 
        printf("\n"); 
    } 
    return 0; 
}

Output:

image 111

Hollow Pyramid

Below is the code for your reference to print a hollow pyramid.

#include <stdio.h> 
int main() 

    int rows = 5; 
    // first outer loop 
    for (int i = 0; i < rows; i++) { 
        // first inner loop 
        for (int j = 0; j < 2 * (rows - i) - 1; j++) { 
            printf(" "); 
        } 
        // second inner loop to print stars * and inner spaces
        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; 
}

Output:

image 112

Butterfly Pattern

Below is the code for your reference to print a butterfly pattern.

#include <stdio.h>
#include <math.h>
void Star(int n)
{
while (n--)
{
printf("* ");
}
}
void Spaces(int n)
{
while (n--)
{
printf("  ");
}
}
int main()
{
int height;
printf("Enter the height of the pattern: ");
scanf("%d", &height);
printf("The pattern of height %d: \n\n", height);
int spaces = 2 * (height - 1);
for (int i = 1; i <= height; i++)
{
Star(i);
Spaces(spaces);
Star(i);
printf("\n");
spaces -= 2;
}
spaces = 2;
for (int i = height - 1; i > 0; i--)
{
    Star(i);
Spaces(spaces);
Star(i);
printf("\n");
spaces += 2;
}
return 0;
}

Output:

image 113

Rhombus Pattern

Below is the code for your reference to print a rhombus pattern.

#include <stdio.h> 
int main() 

    int rows = 5; 
    // first outer loop 
    for (int i = 0; i < rows; i++) { 
      // first inner loop for white spaces 
        for (int j = 0; j < rows - i - 1; j++) { 
            printf(" "); 
        } 
        // second inner loop for * 
        for (int k = 0; k < rows; k++) { 
            printf("* "); 
        } 
        printf("\n"); 
    } 
    return 0; 
}

Output:

image 114

Advanced C Patterns

Advanced C patterns refer to sophisticated and complex structures created using the C programming language. These patterns use various language features, including pointers, memory allocation, loops, and conditional statements, to craft complex designs and solutions. They often involve manipulations of data structures, such as arrays, linked lists, trees, and graphs, showcasing the versatility and power of C. These patterns are commonly used in optimizing algorithms, implementing data structures, or enhancing program efficiency. Mastering these patterns requires a deep understanding of C’s fundamentals and a creative approach to problem-solving within the language’s constraints.

Spiral Pattern

Have a look at the code given below for displaying a spiral pattern.

#include <stdio.h>
void printSpiral(int size)
{
    int row = 0, col = 0;
    int boundary = size - 1;
    int sizeLeft = size - 1;
    int flag = 1;
    char move = 'r';
    int matrix[size][size];
    for (int i = 1; i < size * size + 1; i++)
    {
        matrix[row][col] = i;
        switch (move)
        {
        case 'r':
            col += 1;
            break;
        case 'l':
            col -= 1;
            break;
        case 'u':
            row -= 1;
            break;
        case 'd':
            row += 1;
            break;
        }
        if (i == boundary)
        {
            boundary += sizeLeft;
            if (flag != 2)
            {
                flag = 2;
            }
            else
            {
                flag = 1;
                sizeLeft -= 1;
            }
            switch (move)
            {
            case 'r':
                move = 'd';
                break;
            case 'd':
                move = 'l';
                break;
            case 'l':
                move = 'u';
                break;
            case 'u':
                move = 'r';
                break;
            }
        }
    }
    for (row = 0; row < size; row++)
    {
        for (col = 0; col < size; col++)
        {
            int n = matrix[row][col];
            if (n < 10)
                printf("%d ", n);
            else
                printf("%d ", n);
        }
        printf("\n");
    }
}
int main()
{
    int size = 5;
    printSpiral(size);
    return 0;
}

Output:

image 115

Cross Pattern

Have a look at the code given below for displaying a cross pattern:

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

Output:

image 116

Arrow Pattern

Have a look at the code given below for displaying an arrow pattern:

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

Output:

image 117

Hourglass Pattern

Have a look at the code given below for displaying an hourglass pattern:

#include <stdio.h> 
int main() 

    int rows = 5; 
    // first outer 
    for (int i = 0; i < 2 * rows - 1; i++) { 
        int temp; 
        if (i < rows) { 
            temp = 2 * i + 1; 
        } 
        else { 
            temp = 2 * (2 * rows - i) - 3; 
        } 
        // loop to print leading spaces 
        for (int j = 0; j < temp; j++) { 
            printf(" "); 
        } 
        // loop to print star * 
        for (int k = 0; k < 2 * rows - temp; k++) { 
            printf("* "); 
        } 
        printf("\n"); 
    } 
    return 0; 
}

Output:

image 118

Zig Zag Pattern

Have a look at the code given below for displaying a zig-zag pattern:

#include <stdio.h>
int main() {
    int charactersInLine, zigzagLines;
    printf("Number of characters in a line: ");
    scanf("%d", &charactersInLine);
    printf("Enter the number of zigzag lines: ");
    scanf("%d", &zigzagLines);
    for (int i = 1; i <= zigzagLines; i++) {
        for (int r = 1; r <= charactersInLine; r++) {
            for (int c = 1; c <= charactersInLine; c++) {
                if (r == c) {
                    printf("%d ", r);
                } else {
                    printf("  ");
                }
            }
            printf("\n");
        }
        for (int r = 1; r <= charactersInLine; r++) {
            for (int c = 1; c <= charactersInLine; c++) {
                if (c <= (charactersInLine + 1 - r)) {
                    if (c == (charactersInLine + 1 - r)) {
                        printf("%d ", r);
                    } else {
                        printf("  ");
                    }
                }
            }
            printf("\n");
        }
    }
    return 0;

Output:

image 119

Also, check our blog on Writing C Program for Matrix Multiplication.

About the Author

Senior Consultant Analytics & Data Science, Eli Lilly and Company

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration.