The Ultimate Pattern Programs in C

by  
Share:
The Ultimate Pattern Programs in C

Students preparing to learn the C programming language and want to clear their concepts regarding loop construct, decision-making statements & control statements through coding various number and star pattern programs in C should read this book.

The only way to learn a new programming language is by writing programs in it. – Dennis Ritchie (Creator of C)

Book Overview

I am pleased to inform you that this book contains 100 programs with source codes available in separate text files arranged as per their order.

The book is divided into five sections arranged in a very easy-to-learn manner. After reviewing and practicing everything inside the book, I make certain you will master this topic.

  1. Warmup Exercise – This section has 5 programs to help you understand the working of for loops, grid systems, and star pattern programs in C.
  2. Geometrical Set – This section has 30 programs. I have organized them in easy-to-learn order, i.e., the simple pattern first and the complex pattern last. You can print various geometrical patterns, like pyramid patterns, half pyramid patterns, diamond patterns, hollow diamond patterns, and so on.
  3. Numbers Set – This section consists of 10 programs. You will be printing all the numbers from 0 to 9 with the star pattern.
  4. Alphabet Set – There are 26 programs in this section. You will learn to print letters from A to Z with star patterns.
  5. Mixed Set – There are 29 programs in this section. It has various programs like pyramid pattern of numbers, reverse pyramid patterns, inverted pyramid patterns, hollow diamond pattern programs in C.

If you want a book that covers every aspect of pattern programs in C, including loops, break, and continue statements.

Don’t waste your time looking for anything else. Purchase this book at the cost of less than a cup of coffee.

Buy now

Programs from the Book

I am listing down a few programs from the book, and if you like the content, you can purchase the book.

C program to print table from 1 to 10

C program to print table from 1 to 10

In this C program, we need to print the table from 1 to 10. But before we get to code, let us first understand the program logic.

  • Take ten rows and ten columns.
  • Start an outer loop (row) that iterate ten times.
  • Under the outer loop start an inner loop (column), that iterates 10 times.
  • Multiply the current row value with the current column value inside the inner loop.
#include <stdio.h>	
int main()
{
    int row, column; 
    for (row = 1; row <= 10; row++) {
    	for(column = 1; column <= 10; column++) {
    		printf("%3d ", (row * column));
		}
		printf("\n");
    }
	return 0;
}

Program to print square pattern in C

print square with star pattern

To print the square with star pattern, use the logic given below.

  • Take five rows and five columns.
  • Start an outer loop (row) that will iterate five times.
  • Under the outer loop start an inner loop (column) that iterate the same number of times
  • Inside the inner loop print star.
#include <stdio.h>	
int main()
{
    
    int row, column; 
    int row_length = 5;
    int column_length = 5; 
    
    for (row = 1; row <= row_length; row++) {
        for (column = 1; column <= column_length; column++) {
        	printf("*");
        }
        printf("\n");
    }
    
    return 0;
}

Program to print square with number pattern in C

Print square with number pattern

  • Take five rows and five columns.
  • Start an outer loop (row) that will iterate five times.
  • Under the outer loop start an inner loop (column) that iterate the same number of times
  • Inside the inner loop print current column value.
#include <stdio.h>	
int main()
{
    
    int row, column; 
    int row_length = 5;
    int column_length = 5; 
    
    for (row = 1; row <= row_length; row++) {
        for (column = 1; column <= column_length; column++) {
        	printf("%d", column);
        }
        printf("\n");
    }
    
    return 0;
}

Print square with alphabet pattern program in C

Print square with alphabet pattern

  • Take five rows and five columns.
  • Start an outer loop (row) that will iterate five times.
  • Under the outer loop start an inner loop (column) that iterate the same number of times
  • The ASCII code for capital A is 65 and small a is 97. Use whichever you prefer.
  • Set a variable ascii_code_start to 65 and increment by 1 after each row iteration.
#include <stdio.h>	
int main()
{	
    int row, column; 
    int row_length = 5;
    int column_length = 5; 
    int ascii_code_start = 65;
    
    for (row = 1; row <= row_length; row++) {
        for (column = 1; column <= column_length; column++) {
        	printf("%c", ascii_code_start);
        }
        ascii_code_start++;
        printf("\n");
    }
    return 0;
}

Program to print rhombus with star pattern in C

print rhombus with star pattern

  • The blank spaces in the grid (marked in yellow) will come in play here.
  • Observe, as rows increase, space decreases. Row 1 has 5 spaces; Row 2 has 4 spaces.
  • Start an outer loop (row) that will iterate five times.
  • Under the outer loop, start an inner loop (for spaces) that iterate till total row length minus current row value.
  • After the inner loop for spaces, start loop again (for star) that iterate five times.
  • Print star inside this loop.
#include <stdio.h>	
int main()
{
    int row, column, spaces; 
    int row_length = 10;
    
    for (row = 0; row < row_length; row++) {
    	for (spaces = 1; spaces <= row_length - row ; spaces++) {
        	printf(" ");
        }
        for (column = 1; column <= row_length ; column++) {
        	printf("*");
        }
        printf("\n");
    }
    return 0;
}

Program to print Right-angle triangle pattern in C

print Right-angle triangle pattern

  • Take ten rows and ten columns.
  • As the rows increase column value increases. See Row 1 has 1 star, Row 2 has 2 stars.
  • Start an outer loop (row) that will iterate 10 times.
  • Under the outer loop start an inner loop (column) that iterate the same number of times
  • Inside the inner loop print star.
#include <stdio.h>	
int main()
{
    int row, column; 
    int row_length = 10;
    
    for (row = 1; row <= row_length; row++) {
        for (column = 1; column <= row; column++) {
        	printf("*");
        }
        printf("\n");
    }
    return 0;
}

Program to print right-angle triangle with number pattern in C

Program to print right angle triangle with number pattern in C

As you can see, I have mentioned both the program output and the program logic with a side-by-side comparison to help understand the program.

  • Take ten rows and ten columns.
  • Observe, as the rows increase, column value increases. Row one has one star & Row two has two stars.
  • Start an outer loop (row) that will iterate ten times.
  • Under the outer loop, start an inner loop (column) that iterate the same number of times.
  • Inside the inner loop print column value.
#include <stdio.h>	
int main()
{
    int row, column; 
    int row_length = 10;
    
    for (row = 1; row <= row_length; row++) {
        for (column = 1; column <= row; column++) {
        	printf("%d", column);
        }
        printf("\n");
    }
	return 0;
}

Program to print inverted Right-angle triangle pattern in C

print inverted Right-angle triangle pattern

Similar to the previous program, the logic to print am inverted right-angle triangle with number pattern in Java is almost the same.

  • Take ten rows and ten columns.
  • As the rows increase column value decreases. See Row 1 has 10 star, Row 2 has 9 stars.
  • Start an outer loop (row) that will iterate 10 times.
  • Under the outer loop start an inner loop (column) that iterate the row length minus current row value.
  • Inside the inner loop print star.
#include <stdio.h>	
int main()
{
    int row, column; 
    int row_length = 10;
    
    for (row = 0; row < row_length; row++) {
        for (column = 0; column < row_length - row; column++) {
        	printf("*");
        }
        printf("\n");
    }
    return 0;
}

Program to print inverted right-angle triangle with number pattern in C

Print inverted right-angle triangle with number pattern program in Java

Exactly same as the previous program, we just print numbers instead of stars.

  • Take ten rows and ten columns.
  • Observe, as the rows increase, column decreases.
  • Initialize a counter variable with 1.
  • Print the counter value inside inner loop.
  • After each row iteration, increment counter by 1.
#include <stdio.h>	
int main()
{	
    int row, column; 
    int row_length = 10;
    int counter = 1;
    
    for (row = 0; row < row_length; row++) {
        for (column = 0; column < row_length - row ; column++) {
        	printf("%d", counter);
        }
        counter++;
        printf("\n");
    }
    return 0;
}

Program to print mirror Right-angle triangle pattern in C

print mirror Right-angle triangle pattern

  • Take 10 rows and 10 columns.
  • The blank spaces in the grid (marked in yellow) will come in play here.
  • Observe, as rows increase, space decreases. Row 1 has 9 spaces; Row 2 has 8 spaces.
  • Again, as rows increases column (star) value increases. Row 1 has 10 stars; Row 2 has 9 stars.
  • Start an outer loop (row) that will iterate 10 times.
  • Under the outer loop, start an inner loop (for spaces) that iterate till total row length minus current row value.
  • After the inner loop for spaces, start loop again (for star) that iterate till current
    row value. Print star inside this loop.
#include <stdio.h>	
int main()
{
    int row, column, spaces; 
    int row_length = 10;
    
    for (row = 1; row <= row_length; row++) {
    	for (spaces = 0; spaces < row_length - row ; spaces++) {
        	printf(" ");
        }
        for (column = 1; column <= row ; column++) {
        	printf("*");
        }
        printf("\n");
    }
    return 0;
}

Program to print inverted mirror Right-angle triangle pattern in C

print inverted mirror Right-angle triangle pattern

  • Take ten rows and ten columns.
  • The blank spaces in the grid (marked in yellow) will come in play here.
  • Observe, as rows increase, space increases. Row 1 has 0 space; Row 2 has 1 space.
  • Again, as rows increases column (star) value decreases. Row 1 has 10 stars; Row 2 has 9 stars.
  • Start an outer loop (row) that will iterate 10 times.
  • Under the outer loop, start an inner loop (for spaces) that iterate till current row value.
  • After the inner loop for spaces, start loop again (for star) that iterate till total row length minus current row value. Print star inside this loop.
#include <stdio.h>	
int main()
{
    int row, column, spaces; 
    int row_length = 10;
    
    for (row = 0; row < row_length; row++) {
    	for (spaces = 0; spaces <  row ; spaces++) {
        	printf(" ");
        }
        for (column = 0; column < row_length - row ; column++) {
        	printf("*");
        }
        printf("\n");
    }
    return 0;
}

Print number three with star pattern program in C

Print number three with star pattern program in C

With the side by side-by-side comparison as well as color coding, I have marked the coding logic.

  • Take eleven rows and ten columns, for the 6th row will be the exact middle.
  • On the first, middle and second-to-last row (marked with green), print star in all
    columns, use this condition (row == 1 || row == row_middle || row == row_length-1)
  • From second till second-to-last row (marked with red), print star at the last column, use this condition column == column_length
  • If the condition does not satisfy, print spaces.
#include <stdio.h>	
int main() {
    int row, column; 
    int row_length = 11;
    int column_length = 10; 
    int row_middle = 6; 
	
    for (row = 1; row <= row_length; row++) {
        for (column = 1; column <= column_length; column++) {
        	if(row == 1 || row == row_middle || row == row_length ) {
        		printf("*");
			} else if(column == column_length ) {
        		printf("*");	
			} else {
				printf(" ");	
			}
        	
        }
        printf("\n");
    }   
	return 0;
}

C program to print letter F with star pattern

C program to print letter F with star pattern

With the side by side-by-side comparison as well as color coding, understand the coding logic.

  • Take eleven rows and ten columns, for the 6th row will be the exact middle.
  • On the first and middle (marked with green), print star in all columns, use this condition (row == 1 || row == row_middle)
  • From star in first column of each (marked with red), use this condition column == 1
  • If the condition does not satisfy, print spaces.
#include <stdio.h>	
int main() {
    int row, column; int row_length = 11;
    int column_length = 10; int row_middle = 6; 
	
    for (row = 1; row <= row_length; row++) {
        for (column = 1; column <= column_length; column++) {
        	if(row == 1 || row == row_middle ) {
        		printf("*");
			} else if(column == 1 ) {
        		printf("*");	
			} else {
				printf(" ");	
			}
        }
        printf("\n");
    }
    return 0;
}

Print letter Z with star pattern program in C

print letter Z with star pattern

With the side by side-by-side comparison as well as color coding, understand the coding logic.

  • Take ten rows and ten columns.
  • On the first and last row (marked with green), print star descending order, use this condition (row == 1 || row == row_length)
  • From second till second-to-last row in first column of each (marked with red), use this condition column < row_length-row
  • If the condition does not satisfy, print spaces.
#include <stdio.h>	
int main() {
    int row, column; 
    int row_length = 10; int column_length = 10;  
    
    for (row = 1; row <= row_length; row++) {
        for (column = 1; column <= column_length; column++) {
        	if(row == 1 || row == row_length ) {
        		printf("*");
            } else if(column == (row_length-row)+1) {
        		printf("*");
            } else {
                printf(" ");	
            }
        }
        printf("\n");
    }
    return 0;
}

Program to print pyramid pattern in C

print pyramid with star pattern

To print star with pyramid program in Java, logic is same as printing right angle triangle. The trick here is playing with spaces.

  • Observe, as rows increases spaces decreases (marked in yellow). Row 1 has 10 stars; Row 2 has 9 stars.
  • Again, as the rows increase column (stars) increases. See Row 1 has 1 star, Row 2 has 2 stars.
  • Start an outer loop (row) that will iterate five times.
  • Under the outer loop, start an inner loop (for spaces) that iterate till total row length minus current row value.
  • After the inner loop for spaces, start loop again (for star) that iterate till current row value.
  • Print star inside this loop
#include <stdio.h>	
int main()
{	
    int row, column, spaces; 
    int row_length = 10;
    
    for (row = 1; row <= row_length; row++) {
    	for (spaces = 0; spaces < row_length - row ; spaces++) {
        	printf(" ");
        }
        for (column = 1; column <= row ; column++) {
        	printf("* ");
        }
        printf("\n");
    }
    return 0;
}

C program to print pyramid pattern of numbers

C program to print pyramid pattern of numbers

With the side-by-side comparison and color coding, you can understand there are two triangles in this pyramid pattern.

  • Break the program in two section.
  • The left section (marked with green), has spaces and value. Here, number of rows is equals to number of columns. After printing the spaces use value_print to print the output and increment it by 1.
  • Adjust the value_print by decrementing it by 2.
  • The right section (marked with red), number of columns is one less than number of rows.
  • Inside the inner loop, print value_print variable and decrement it by 1.
#include <stdio.h>	
int main() {
    int row, column1, column2, spaces; 
    int row_length = 5; int value_print = 1;
    
    for (row = 1; row <= row_length; row++) {
    	for (spaces = 1; spaces <= row_length-row; spaces++) {
    		printf(" ");
    	}
    	value_print = row;
        for (column1 = 1; column1 <= row; column1++) {
        	printf("%d", value_print);
        	value_print++;
        }
        value_print -= 2;
        for (column2 = 1; column2 < row; column2++) {
        	printf("%d", value_print);
        	value_print--;
        }
        printf("\n");
    }
    return 0;
}

Program to print hollow diamond with star pattern program in C

Program to print hollow diamond with star pattern program in c

Look at the side-by-side comparison and understand what we are going to code.

  • Break the program in two section.
  • The top section (marked with green), is a hollow pyramid.
  • The bottom section (marked with red), is an inverted hollow pyramid.
#include <stdio.h>	
int main()
{	
    int row, column, spaces; 
    int row_length = 5;
    
    // upper section
    for (row = 1; row <= row_length; row++) {
    	for (spaces = 1; spaces <= row_length-row ; spaces++) {
        	printf(" ");
        }
        for (column = 1; column <= row; column++) {
        	if (column == 1 || column == row) {
        		printf("* ");
			} else {
				printf("  ");
			}
        }
		printf("\n");    
    }
    
    // lower section
    for (row = 1; row < row_length; row++) {
    	for (spaces = 0; spaces < row ; spaces++) {
        	printf(" ");
        }
        for (column = 0; column < row_length-row; column++) {
        	if (row == (row_length-1) ) {
        		printf("* ");
			} else if (column == 0 || column == (row_length-row)-1 ) {
        		printf("* ");
			} else {
				printf("  ");
			}
        }
		printf("\n");    
    }
	return 0;
}

Program to print pascal triangle in C

Program to print pascal triangle in Java

With the side-by-side comparison and color coding, you can understand there are two triangles in this pyramid pattern.

  • In Pascal’s triangle, each number is the sum of the two numbers directly above it. Read more here.
  • For first row and first column of each row, initialize the value as 1.
  • For the rest calculate by v = v * (row - column + 1) / column
#include <stdio.h>	
int main() {
    int row, column, spaces; 
    int row_length = 6; int v;
    
    for (row = 0; row < row_length; row++) {
    	for (spaces = 1; spaces < row_length - row ; spaces++) {
        	printf(" ");
        }
        for (column = 0; column <= row ; column++) {
        	if(column == 0 || row == 0) {
        		v = 1;
            } else {
                v = v * (row-column + 1)/column;
            }
        	printf("%d ",v);
        }
        printf("\n");
    }    
    return 0;
}

Print alphanumeric pattern program in C

 print alphanumeric pattern

To print the alphanumeric pattern given above, use the logic mention below.

  • Observe, starting with ASCII value of A (marked in green) combining with current row number (marked in red).
  • While iterating column wise, character and number both increments by 1.
  • Print character and digit separately.
#include <stdio.h>	
int main() {
    int row, column; int row_length = 5;
    int ascii_code_start = 65; char print_value;
    
    for (row = 1; row <= row_length; row++) {
        for (column = 0; column <= row_length-row; column++) {
        	//print_value = (ascii_code_start + column )-1;
        	printf("%c", (ascii_code_start+row+column)-1);
        	printf("%d", (row+column));
        	printf(" ");
        }
        printf("\n");
    }
    return 0;
}

Program to print Floyd’s triangle in C

print Floyd triangle

To print the floyd’s triangle in C, use the logic given below:

  • Observe, at each iteration value increments by 1.
  • Use a counter that increments by 1 inside inner loop.
#include <stdio.h>	
int main() {
    int row, column; 
    int row_length = 5;
    int counter = 1;
    
    for (row = 1; row <= row_length; row++) {
        for (column = 1; column <= row; column++) {
        	printf("%d ", counter);
        	counter++;
        }
        printf("\n");
    }
    return 0;
}

Claim this exclusive offer now

Don’t waste your time looking for anything else. Purchase this book at the cost of less than a cup of coffee.

Buy now

Get the latest updates directly in your inbox for FREE: Subscribe here

Share your feedback / let me know your doubts regarding this tutorial in the comment box given below.

Share:
Hasan

Shahrukh Khan (Hasan)

A software engineer who's a die-hard coder, blogger, dreamer and mentor with years of expertise in web development. Know more...

Related Posts

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.