Most Frequently Asked C Pattern Programs You Need to Know

C pattern programs are critical in every interview question. Most of us just give up when it comes to pattern programs. But these questions will make you understand practically, based on mathematical logic and matrices' fundamentals.   

This article will cover star patterns, number patterns, alphabet patterns, and some unique pattern programs.

Let’s deal with different C Pattern programs through the following docket.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Star Pattern

Pattern 1: Right-Angle Triangle

C_Pattern_Programs_1.

#include <stdio.h>

int main()

 {

int rows = 5;

for (int i = 1; i <= rows; ++i) //Outer loop for rows

                       {

                    for (int j = 1; j <= i; ++j)

                            {  //Inner loop for Col

printf("* "); //Print *

}

printf("\n"); //New line

}

}

Pattern 2: Inverted Right-Angle Triangle in a Start Pattern

C_Pattern_Programs_2

#include <stdio.h>

int main()

 {

int i, rows = 5;

            for( i = rows; i >= 1; --i)

            {  //For Loop for Row 

            for(int j = 1; j <= i; ++j)

            {

              printf("* ");

}

            printf("\n"); //New line

 }

}

Pattern 3: Arrow-Shaped Pattern

C_Pattern_Programs_3

#include <stdio.h>

int main()

 {

int i, rows = 5;

            for (int i = 0; i <= rows - 1; i++) { //For Loop for Row 

for (int j = 0; j <= i; j++) { //For Loop for Col

printf("*" " "); //prints * and blank space

}

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

}

for (int i = rows - 1; i >= 0; i--) { //For Loop for Row

for (int j = 0; j <= i - 1; j++) { //For Loop for Col

printf("*" " "); //prints * and blank space

}

            printf("\n"); //New line

 }

}

Learn From The Best Mentors in the Industry!

Automation Testing Masters ProgramExplore Program
Learn From The Best Mentors in the Industry!

Pattern 4: Mirrored Right-Angle Triangle

C_Pattern_Programs_4

#include <stdio.h>

int main()

 {

int i, j, rows = 5;

            for (i = 0; i < rows; i++) {  

for (j = 2 * (rows - i); j >= 0; j--) { //For Loop for Row

printf(" "); // Print Spaces

}

for (j = 0; j <= i; j++) { //For Loop for col

 //For Loop for Col

printf("*" " "); //prints * and blank space

}

            printf("\n"); //New line

 }

}

Pattern 5: Inverted Mirrored Right-Angle Triangle

C_Pattern_Programs_5

#include <stdio.h>

int main()

 {

int i, j,k, rows = 5;

           for (int i = rows; i >= 1; i--) { 

for (int j = rows; j > i; j--) { 

printf(" "); // Prints Blank space

}

for (int k = 1; k <= i; k++) {

printf("*"); //prints * and blank space

}

            printf("\n"); //New line

 }

}

Pattern 6: Mirrored Arrow-Shaped Star Pattern

C_Pattern_Programs_6

#include <stdio.h>

int main()

 {

int i, j,k, rows = 5;

           for (int i = 1; i <= rows; i++) {

for (int j = i; j < rows; j++) { //Rows Loop

printf(" "); // Blank Space

}

for (int k = 1; k <= i; k++) { //Cols Loop

printf("*"); // Prints *

}

printf("\n");

}

for (int i = rows; i >= 1; i--) {

for (int j = i; j <= rows; j++) { //Rows Loop

printf(" ");  // Prints blank spaces

}

for (int k = 1; k < i; k++) { //Col Loop

printf("*"); //prints * and blank space

}

            printf("\n"); //New line

 }

}

Learn 15+ In-Demand Tools and Skills!

Automation Testing Masters ProgramExplore Program
Learn 15+ In-Demand Tools and Skills!

Pattern 7: Pyramid Star Pattern Program

C_Pattern_Programs_7

#include <stdio.h>

int main()

 {

int i, j, rows = 5;

           for (int i = 0; i < rows; i++) {

for (int j = rows - i; j > 1; j--) { //Loop for blank space

printf(" "); //Print Space

}

for (int j = 0; j <= i; j++) { //loop for star

    printf("* "); //prints * and blank space

}

            printf("\n"); //New line

 }

}

Pattern 8: Inverted Pyramid

C_Pattern_Programs_8

#include <stdio.h>

int main()

 {

int i, j,k, rows = 5;

           for (int i = 0; i <= rows - 1; i++) { //For loop for Rows

for (int j = 0; j <= i; j++) { //For loop for Col

printf(" "); // blank space

}

for (int k = 0; k <= rows - 1 - i; k++) { 

printf("*"  " "); //prints * and blank space

}

            printf("\n"); //New line

 }

}

Unleash a High-paying Automation Testing Job!

Automation Testing Masters ProgramExplore Program
Unleash a High-paying Automation Testing Job!

Pattern 9: Diamond Star Pattern

C_Pattern_Programs_9

#include <stdio.h>

int main()

 {

int  j,x, rows=5 ,blank=1;

blank = rows - 1;

          for (j = 1; j <= rows; j++) {

for (x = 1; x <= blank; x++) {

printf(""); //print blank space

}

blank--;

for (x = 1; x <= 2 * j - 1; x++) {

printf("*"); //prints * and blank space

}

            printf("\n"); //New line

 }

blank = 1;

for (j = 1; j <= rows - 1; j++) {

for (x = 1; x <= blank; x++) {

printf(" "); //Print Spaces

}

blank++;

for (x = 1; x <= 2 * (rows - j) - 1; x++) {

printf("*"); //Print Star

}

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

}

}

Become an Automation Test Engineer in 11 Months!

Automation Testing Masters ProgramExplore Program
Become an Automation Test Engineer in 11 Months!

Pattern 10: Upper Inverted Pyramid and Lower Pyramid

C_Pattern_Programs_10.

#include <stdio.h>

int main()

 {

int rows=5 ;

          for (int i = 0; i <= rows - 1; i++) {

for (int j = 0; j < i; j++) {

printf(" "); //Print blank space

}

for (int k = i; k <= rows - 1; k++) {

printf("*"  " "); //Print star and blank space

}

printf("\n"); //New line

}

//For lower Pyramid

for (int i = rows - 1; i >= 0; i--) {

for (int j = 0; j < i; j++) {

printf(" "); //Print spaces

}

for (int k = i; k <= rows - 1; k++) {

printf("*"  " "); //Print Star and Space

}

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

}

}

Pattern 11: Right Diagonal Star Pattern

C_Pattern_Programs_11

#include <stdio.h>

int main()

 {

          int i, j;

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

for (j = 0; j < 5 - i; j++) {

printf(" "); //Print blank space

}

printf("*\n");//Print new line

}

}

Pattern 12: X Star Pattern 

C_Pattern_Programs_12.

#include <stdio.h>

int main()

 {

          int i, j, rows =5;

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

for (j = i; j < rows; j++) {

printf(" ");//print spaces

}

for (j = 1; j <= (2 * i - 1); j++) {

if (j == 1 || j == (2 * i - 1))//Logic for printing star

printf("*");

else

printf(" ");//if logic fails print space

}

printf("\n");

}

//Lower Inverted V pattern

for (i = 2; i <= rows; i++) {

for (j = i; j < rows; j++) {

printf(" ");//Print spaces

}

for (j = 1; j <= (2 * i - 1); j++) {

if (j == 1 || j == (2 * i - 1))//Logic for printing star

printf("*");

else

printf(" ");//if logic fails print space

}

printf("\n");

}

}

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Pattern 13: Inverted v Star Pattern

C_Pattern_Programs_13

#include <stdio.h>

int main()

{

int out, in;

int rows = 5;

int x = 5;

int y = 5;

for (out = 1; out <= rows; out++) {

for (in = 1; in <= rows * 2; in++) {

if (in == x || in == y) {

printf("*");

} else {

printf(" ");

}

}

x--;

    y++;

printf("\n");

}

}

Pattern 14: V Star Pattern

C_Pattern_Programs_14

#include <stdio.h>

int main()

 {

int n = 5;

int i, j;

for (i = n - 1; i >= 0; i--) {

for (j = n - 1; j > i; j--) {

printf(" "); //Print Space

}

printf("*"); //Print star

for (j = 1; j < (i * 2); j++)

printf(" ");//Print space

if (i >= 1)

printf("*");

printf("\n");//Enter newline

}

}

Pattern 15: Pyramid Star Pattern

C_Pattern_Programs_15

#include <stdio.h>

int main()

 {       

int rows = 5;

int i,j,k;

for (int i = 1; i <= rows; i++) {

for (int j = i; j < rows; j++) {

printf(" ");

}

for (int k = 1; k <= (2 * i - 1); k++) {

if (k == 1 || i == rows || k == (2 * i - 1)) {

//Logic for printing Pattern

printf("*"); //Print Star

} else {

printf(" ");  //Print Spaces

}

}

printf("\n");

}

}

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Pattern 16: Inverted Star Pyramid

C_Pattern_Programs_16.

#include <stdio.h>

int main()

 {          

int rows = 5;

int i,j,k;

//Row input

for (int i = rows; i >= 1; i--) {

for (int j = i; j < rows; j++) {

printf(" "); //Print Spaces

}

for (int k = 1; k <= (2 * i - 1); k++) {

if (k == 1 || i == rows || k == (2 * i - 1)) { //logic to print Pattern

printf("*"); //Ture prints star

} else {

printf(" "); //False prints spaces

}

}

printf("\n");

}

}

Pattern 17: Hollow Square Pattern

C_Pattern_Programs_17 

#include <stdio.h>

int main()

 {       

int n=10,m=10;

int i, j;

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

for (j = 1; j <= m; j++) {

if (i == 1 || i == n || j == 1 || j == m) //Logic to print 

printf("*"); //Tue?, print star

else

printf(" "); //Tue?, print space

}

printf("\n");

}

}

Pattern 18: Hollow Square Pattern

C_Pattern_Programs_18 

#include <stdio.h>  

int main()  

{  

    int i,j, rows=10;     

    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;  

}  

Pattern 19: Square Star Pattern

C_Pattern_Programs_19.

#include <stdio.h>  

 void main()  

{  

    int rows=5;      

    for(int i=0;i<rows;i++)  

    {  

        for(int j=0;j<rows;j++)  

        {  

            printf("*");  

        }  

        printf("\n");  

    }        

}  

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Pattern 20: Hollow Rhombus Star Pattern

C_Pattern_Programs_20.

#include <stdio.h>  

int main()  

{  

    int i,j,k,rows=5;      

    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;  

}  

Pattern 21: Half Diamond Star Pattern

C_Pattern_Programs_21

#include<stdio.h>

int main()  

{  

    int rows=5,i,j;  

    //prints upper half diamond

    for( i=1;i<=rows;i++)  

    {  

      for( j=1;j<=i;j++)

      {

          printf("*");

      }

      printf("\n"); 

     } 

    //prints lower-half diamond

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

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

           printf("*");  

            }  

       printf("\n");  

    }     

    return 0;  

}

Following, let’s have a look at the number pattern programs in c

Number Pattern

Pattern 22: Right-Angle Triangle Number Pattern

C_Pattern_Programs_22

#include <stdio.h>

int main()

 {

int rows = 5;

int i, j, k = 1;

for (int i = 1; i <= rows; i++) {

for (int j = 1; j <= i; j++) {

printf("%d", j); //Print j value and space

}

printf("\n");

}

}

Pattern 23: Floyd’s Triangle

C_Pattern_Programs_23

#include <stdio.h>

int main()

 {

int rows = 5;

int i, j, k = 1;

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

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

printf("%d",k++); //Floyd’s triangle logic(prints K value and increments on every iteration)

}

printf("\n");

}

}

Pattern 24: Pascals Program

C_Pattern_Programs_24.

#include<stdio.h>

void main() {

    int rows=5, temp=1,i,j,k;

    for (i=0; i<rows; i++) {

        for (j=1; j<= rows-i; j++)

            printf(" ");

        for (k=0; k<=i; k++) {

            if (k==0 || i==0)

                temp = 1;

            else

                temp=temp*(i-k+1)/k;

            printf("%2d", temp);

        }

        printf("\n");

    }

Pattern 25: Square Number Pattern

C_Pattern_Programs_25.

#include <stdio.h>

int main()

 {

int rows = 5;

int i, j;

for (int i = 1; i <= rows; i++) {

int num;

if (i % 2 == 0) {

num = 0;

for (int j = 1; j <= rows; j++) {

printf("%d",num);

num = (num == 0) ? 1 : 0;

}

} else {

num = 1;

for (int j = 1; j <= rows; j++) {

printf("%d",num);

num = (num == 0) ? 1 : 0;

}

}

printf("\n");

}

}

Let’s move ahead with the alphabet pattern programs.

Alphabet Pattern

Pattern 26: Right-Angle Triangle Alphabet Pattern

C_Pattern_Programs_26

#include<stdio.h>

void main()

{

    int i,j,rows=5;

    int alphabet=64; 

    for(i=1;i<=rows;i++)

    {

        for(j=1;j<=i;j++)

        {

            printf("%c",(char)(j+alphabet));

        }

        printf("\n");

    }

}

Pattern 27: Inverted Right-Angle Triangle

C_Pattern_Programs_27

#include <stdio.h>

int main()

{

    int rows=5;

    int alphabet = 65; 

    for (int i = rows - 1; i >= 0; i--)

    {

        for (int j = 0; j <= i; j++)

        {

            printf("%c ", (char)alphabet + j);

        }

        printf("\n");

    }

}

Pattern 28: Pyramid Alphabet Pattern

C_Pattern_Programs_28.

#include <stdio.h>

int main()

{

      int i,j,rows=5;

    int alphabet=64;

    for (int i = 0; i <= 4; i++) {

int alphabet = 65; //ASCII value of “A”

for (int j = 4; j > i; j--) {

printf(" "); //Print Space

}

for (int k = 0; k <= i; k++) {

printf("%2c",(char) (alphabet + k)); //Print Alphabet

}

printf("\n");

}

}

Finally, look at some unique patterns using two symbols, a star and a hyphen.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Special Pattern

Pattern 29: Half Diamond Pattern Using Star and Hyphen Symbol

C_Pattern_Programs_29.

#include<stdio.h>

void main(){

    int i,j,k,rows=5;  

    for(i=1;i<=rows;i++)

    {

        if(i%2==0){

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

            printf("-");

    }

        }else{

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

        printf("*");

    }

        }

printf("\n");

    }

    for(i=rows;i>1;i--)

    {

        if(i%2==0){

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

            printf("*");

        }

        }

        else{

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

        printf("-");

        }

        }

    printf("\n");

    }

}

Pattern 30: Left Half Diamond Pattern 

C_Pattern_Programs_30

#include<stdio.h>

void main(){

    int i,j,k,rows=5;

    for(i=0;i<rows;i++)

    {

    for(j=0;j<rows-i;j++){

        printf(" ");

}

if(i%2==0){

        for(k=0;k<=i;k++){

        printf("*");

    }

        }else{

            for(k=0;k<=i;k++){

        printf("-");

    }

        }

printf("\n");

    }

    for(i=rows-1;i>0;i--)

    {

    for(j=rows;j>=i;j--){

    printf(" ");

    }

    if(i%2==0){

        for(k=i;k>0;k--){

        printf("-");

        }

    }else{

        for(k=i;k>0;k--){

        printf("*");

        }

    }

    printf("\n");

    }   

}  

Pattern 31: Triangle Pattern

C_Pattern_Programs_31

#include<stdio.h>

void main()

{    

int i,j,rows=5,k;

    for(i=0;i<rows;i++){

    for(k=1;k<rows-i;k++){

    printf(" ");

    }

    printf("*");

    for(j=0;j<=i-1;j++){

            printf("-");

    }

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

    printf("-");

    }

    if(i>0){

    printf("*");

    }

    printf("\n");

    }

Pattern 32: Full Diamond

C_Pattern_Programs_32.

#include<stdio.h>

void main(){

    int i,j,k,rows=5;

    for(i=1;i<=rows;i++)

    {

    for(j=1;j<=rows-i;j++){

        printf(" ");

}

if(i%2==1){

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

        printf("*");

    }

        }else{

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

        printf("-");

    }

        }

printf("\n");

    }

    for(i=rows;i>=1;i--)

    {

    for(j=rows;j>=i;j--){

    printf(" ");

    }

    if(i%2==1){

        for(k=2*i-2;k>1;k--){

        printf("-");

        }

    }else{

        for(k=2*i-2;k>1;k--){

        printf("*");

        }

    }

    printf("\n");

    }

}  

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Pattern 33: Christmas Tree Pattern 

C_Pattern_Programs_33.

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <time.h>

#define RefRate 20000

#define randomness 5 

void clrscr()

{

    system("@cls||clear");

}

// Print a random character giving preference 

// to *

void printLeaf()

{

    char ltypes[5] = { '*', '+', 'o', '.', 'O' };

    int temp = rand() % randomness;  

    // Giving preference to *

    if (temp == 1)

        printf("%c ", ltypes[rand() % 5]);

    else

        printf("%c ", ltypes[1]);

}  

void tri(int f, int n, int toth)

{

    int i, j, k = 2 * toth - 2;

  

    for (i = 0; i < f - 1; i++)

        k--;  

    // number of rows

    for (i = f - 1; i < n; i++) {  

        // space handler

        for (j = 0; j < k; j++)

            printf(" ");  

        // decrementing k after each loop

        k = k - 1;  

        // number of columns, printing stars

        for (j = 0; j <= i; j++)

            printLeaf();  

        printf("\n");

    }

}  

// Prints multiple triangles

void printTree(int h)

{

    int start = 1, stop = 0, diff = 3;

    while (stop < h + 1) {

        stop = start + diff;

        tri(start, stop, h);

        diff++;

        start = stop - 2;

    }

}  

// Prints bottom part.

void displayLog(int n)

{

    int i, j, k = 2 * n - 4;  

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

        // space handler

        for (j = 0; j < k; j++)

            printf(" ");  

        for (j = 1; j <= 6; j++)

            printf("#");  

        printf("\n");

    }

}  

// Driver code

int main()

{

    srand(time(NULL));

    int t = 6;  

    printf("\n*********MERRY CHRISTMAS*********\n\n");

    // refresh loop

    while (1) {

        clrscr();

        printTree(t);  

        displayLog(t);

        usleep(RefRate);

    }

    return 0;

}

Next Steps

"C Interview Question" can be your next topic. So far, you have learned the C pattern programs. The following fundamentals will be the data structures and the varieties in data structures used for different purposes.

If you are interested in software development, you can explore Simplilearn's courses that will give you the work-ready software development training you need to succeed today. Are you looking for a comprehensive training program in today's most in-demand software development skills, tools, and languages? Our Full Stack Developer - MERN Stack should be the right thing for your career. Explore the course and enroll soon.

Please let us know in the comment section below if you have questions regarding the "C Pattern Programs ” tutorial. Our experts will get back to you at the earliest.

FAQs

1. What are pattern programs in C?

Pattern programs in C involve using nested loops to print various patterns of characters, numbers, or symbols. These programs help understand the use of loops and conditional statements, which are fundamental concepts in programming. Examples include star patterns, number patterns, and alphabet patterns.

2. How do I write a simple star pattern program in C?

You need to use nested loops to write a simple star pattern program in C. Here’s an example of a basic right-angled triangle star pattern:

#include <stdio.h>

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

This program prompts the user to enter the number of rows and prints a right-angled triangle of stars.

3. What are some common mistakes to avoid when writing pattern programs in C?

Incorrect loop boundaries: Ensure that loop counters are correctly set to start and end at the right values.

Not using nested loops properly: Understand the structure of nested loops to print the correct pattern.

Ignoring edge cases: Consider and handle cases like zero or negative input values.

Misplacing print statements: Ensure that printf statements are correctly placed within the loops to format the output correctly.

4. How can I print a pyramid number pattern in C?

To print a pyramid number pattern, you need to handle spaces and numbers properly in the nested loops. Here’s an example:

#include <stdio.h>

int main() {
    int i, j, space, rows, number = 1;
    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    for(i = 1; i <= rows; ++i) {
        for(space = 1; space <= rows - i; ++space) {
            printf("  ");
        }
        number = 1;
        for(j = 1; j <= 2 * i - 1; ++j) {
            printf("%d ", number);
            if (j < i)
                ++number;
            else
                --number;
        }
        printf("\n");
    }
    return 0;
}

5. Where can I find more practice problems for pattern programs in C?

You can find more practice problems on educational websites like GeeksforGeeks, HackerRank, and LeetCode. Additionally, many programming textbooks and online courses include sections on pattern programs. Joining programming forums and communities can also provide access to a wealth of shared problems and solutions.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Acknowledgement
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, OPM3 and the PMI ATP seal are the registered marks of the Project Management Institute, Inc.