For working professionals
For fresh graduates
More
5. Array in C
13. Boolean in C
18. Operators in C
33. Comments in C
38. Constants in C
41. Data Types in C
49. Double In C
58. For Loop in C
60. Functions in C
70. Identifiers in C
81. Linked list in C
83. Macros in C
86. Nested Loop in C
97. Pseudo-Code In C
100. Recursion in C
103. Square Root in C
104. Stack in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
If you've ever explored the world of C programming, chances are you've come across the term "pattern program in C". These seemingly simple programs are often among the first that beginners encounter, and for good reason. They’re visually rewarding, conceptually rich, and a perfect getaway to mastering loops and conditional logic. That’s why in every career-forwarding software development course, pattern concept is included.
In this blog post, we'll explore what pattern programs in C language are, where they’re used, and cover a wide range of types—complete with commented code examples, outputs, and step-by-step explanations. Whether you're a student sharpening your skills or a developer revisiting the classics, this guide is for you.
A pattern program in C is a program designed to display structured output in the form of shapes or sequences—typically using characters like stars (*), numbers, or alphabets. The patterns are usually printed on the console in rows and columns using nested loops (especially for loops).
Here are some of the applications of pattern programs in C:
Now, let’s look at different types of pattern programs in C, categorized for clarity.
Now, we’ll discover the top pattern program in C, according to their specific categories, starting from the basics of the star pattern program in C.
Before you start with any of the pattern program, you should understand the following concepts:
Among all variations, star patterns are the most popular category of pattern program in C. These patterns are created using asterisks (`*`) and are ideal for mastering nested loops, conditional statements, and flow control logic. Whether you're a beginner or brushing up for an interview, these star-based patterns are must-practice material.
Below are several common types of star pattern programs in C, complete with code, output, and explanations.
This beginner-friendly pattern program in C prints a right-angled triangle aligned to the left. It helps build the foundational logic of nested loops.
Code:
#include <stdio.h>
int main() {
int i, j, rows = 5;
// Outer loop for rows
for(i = 1; i <= rows; i++) {
// Inner loop prints stars equal to current row number
for(j = 1; j <= i; j++) {
printf("* ");
}
printf("\n"); // Move to next line after each row
}
return 0;
}
Output:
*
* *
* * *
* * * *
* * * * *
Explanation:
This pattern program in C is a simple variation where the triangle is flipped vertically, starting with the maximum number of stars and decreasing each row.
Code:
#include <stdio.h>
int main() {
int i, j, rows = 5;
// Outer loop starts from 5 and decrements
for(i = rows; i >= 1; i--) {
// Print i stars on each row
for(j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Output:
* * * * *
* * * *
* * *
* *
*
Explanation:
Also explore Post Graduate Diploma in Management for a high-paying career in the long run
A classic pattern program in C that prints a square with only the border filled using stars. It’s excellent for learning how to use conditionals inside nested loops.
Code:
#include <stdio.h>
int main() {
int i, j;
int size = 5; // Size of the square
// Loop through rows
for(i = 1; i <= size; i++) {
// Loop through columns
for(j = 1; j <= size; j++) {
// Print star at borders only
if(i == 1 || i == size || j == 1 || j == size) {
printf("* ");
} else {
printf(" "); // Empty space inside the square
}
}
printf("\n"); // Move to next row
}
return 0;
}
Output:
* * * * *
* *
* *
* *
* * * * *
Explanation:
This pattern program in C prints a centered pyramid made of stars. It requires understanding how to print spaces before the stars.
Code:
#include <stdio.h>
int main() {
int i, j, k;
int rows = 5;
// Loop for each row
for(i = 1; i <= rows; i++) {
// Print spaces before stars
for(j = i; j < rows; j++) {
printf(" ");
}
// Print stars: 2*i - 1 stars per row
for(k = 1; k <= (2 * i - 1); k++) {
printf("* ");
}
printf("\n"); // Move to next row
}
return 0;
}
Output:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
Explanation:
Must explore Artificial Intelligence & Machine Learning course to fast-forward your career to a high-paying position.
Not all patterns are made of stars, some use numbers to create structured and meaningful visual designs. A number pattern program in C teaches how to work with variables, increments, and mathematical relationships between rows and columns. These are often used in coding rounds and classroom assignments due to their logic-heavy nature.
Let’s explore a few essential number pattern programs in C examples.
This classic pattern program in C prints a triangle of consecutive numbers. Each row has one more element than the previous.
Code:
#include <stdio.h>
int main() {
int i, j, num = 1;
int rows = 5;
// Loop through each row
for(i = 1; i <= rows; i++) {
// Print numbers in each row
for(j = 1; j <= i; j++) {
printf("%d ", num);
num++; // Increment number
}
printf("\n"); // Move to next row
}
return 0;
}
Output:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Explanation:
This pattern program in C creates a centered pyramid made of increasing numbers. It's a good exercise in combining arithmetic with alignment logic.
Code:
#include <stdio.h>
int main() {
int i, j, k;
int rows = 5;
// Loop through each row
for(i = 1; i <= rows; i++) {
// Print leading spaces
for(j = i; j < rows; j++) {
printf(" ");
}
// Print increasing numbers
for(k = 1; k <= i; k++) {
printf("%d ", k);
}
printf("\n"); // Move to next row
}
return 0;
}
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Explanation:
A visually stunning pattern program in C, this one prints rows that form number palindromes, where numbers increase and then decrease symmetrically. To thoroughly understand this logic, you should explore the palindrome in C article.
Code:
#include <stdio.h>
int main() {
int i, j;
for(i = 1; i <= 5; i++) {
// Print leading spaces
for(j = 1; j <= 5 - i; j++) {
printf(" ");
}
// Print decreasing numbers
for(j = i; j >= 1; j--) {
printf("%d ", j);
}
// Print increasing numbers
for(j = 2; j <= i; j++) {
printf("%d ", j);
}
printf("\n"); // Move to next line
}
return 0;
}
Output:
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
Explanation:
A mathematically rich pattern program in C, Pascal’s Triangle prints binomial coefficients in a triangular layout.
Code:
#include <stdio.h>
// Function to calculate factorial
int factorial(int n) {
if(n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
// Function to calculate combination (nCr)
int combination(int n, int r) {
return factorial(n) / (factorial(r) * factorial(n - r));
}
int main() {
int i, j, rows = 5;
for(i = 0; i < rows; i++) {
// Print spaces to align the triangle
for(j = 0; j < rows - i; j++) {
printf(" ");
}
// Print combination values
for(j = 0; j <= i; j++) {
printf("%d ", combination(i, j));
}
printf("\n");
}
return 0;
}
Output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Explanation:
Alphabet patterns are another popular form of pattern program in C, using characters instead of numbers or stars. These are particularly useful for practicing character handling and ASCII values in C. They offer a fun twist by involving both alphabetical logic and structure formatting.
Here are a few commonly used alphabet pattern program in C examples:
This beginner-friendly pattern program in C prints a left-aligned triangle of alphabets, starting from ‘A’ on each row.
Code:
#include <stdio.h>
int main() {
int i, j;
char ch;
// Loop through each row
for(i = 1; i <= 5; i++) {
ch = 'A'; // Reset ch to 'A' for each row
// Print characters from A up to ith character
for(j = 1; j <= i; j++) {
printf("%c ", ch);
ch++; // Move to next character
}
printf("\n");
}
return 0;
}
Output:
A
A B
A B C
A B C D
A B C D E
Explanation:
A simple variation of the above, this pattern program in C prints the alphabet triangle in reverse—starting with maximum characters and reducing with each row.
Code:
#include <stdio.h>
int main() {
int i, j;
char ch;
for(i = 5; i >= 1; i--) {
ch = 'A';
// Print characters from A up to ith character
for(j = 1; j <= i; j++) {
printf("%c ", ch);
ch++;
}
printf("\n");
}
return 0;
}
Output:
A B C D E
A B C D
A B C
A B
A
Explanation:
This unique pattern program in C starts each row with a different letter based on the row number.
Code:
#include <stdio.h>
int main() {
int i, j;
char start;
for(i = 1; i <= 5; i++) {
start = 'A' + i - 1; // Starting character for each row
for(j = 1; j <= i; j++) {
printf("%c ", start);
start++;
}
printf("\n");
}
return 0;
}
Output:
A
B C
C D E
D E F G
E F G H I
Explanation:
This pattern program in C prints characters in reverse alphabetical order, creating an inverted triangle.
Code:
#include <stdio.h>
int main() {
int i, j;
char ch;
for(i = 1; i <= 5; i++) {
ch = 'E'; // Start from 'E'
for(j = 5; j >= i; j--) {
printf("%c ", ch);
ch--;
}
printf("\n");
}
return 0;
}
Output:
E D C B A
E D C B
E D C
E D
E
Explanation:
Grid and square patterns are foundational in pattern programs in C. These patterns allow you to develop logic for printing and formatting within rectangular or square structures. Whether you're printing checkerboards, squares, or hollow grids, mastering these types of patterns builds a solid foundation for advanced programming challenges.
Let’s take a look at a few commonly used grid and square pattern programs in C:
The solid square pattern is one of the simplest pattern programs in C. It prints a square filled entirely with asterisks (`*`).
Code:
#include <stdio.h>
int main() {
int i, j;
int size = 5;
// Outer loop for rows
for(i = 1; i <= size; i++) {
// Inner loop for columns
for(j = 1; j <= size; j++) {
printf("* ");
}
printf("\n"); // Move to next row
}
return 0;
}
Output:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Explanation:
The hollow square pattern is a variation where only the borders of the square are printed, and the inside is left blank. This pattern program in C helps you practice nested loops and conditional statements.
Code:
#include <stdio.h>
int main() {
int i, j;
int size = 5;
// Outer loop for rows
for(i = 1; i <= size; i++) {
// Inner loop for columns
for(j = 1; j <= size; j++) {
// Print '*' on borders
if(i == 1 || i == size || j == 1 || j == size) {
printf("* ");
} else {
printf(" "); // Print space inside
}
}
printf("\n"); // Move to next row
}
return 0;
}
Output:
* * * * *
* *
* *
* *
* * * * *
Explanation:
The checkerboard pattern alternates between stars and spaces, creating a visually appealing grid. It’s another great pattern program in C to practice nested loops and modular arithmetic.
Code:
#include <stdio.h>
int main() {
int i, j;
int size = 8;
// Loop through each row
for(i = 1; i <= size; i++) {
// Loop through each column
for(j = 1; j <= size; j++) {
// Print stars and spaces alternately
if((i + j) % 2 == 0) {
printf("* ");
} else {
printf(" ");
}
}
printf("\n"); // Move to the next row
}
return 0;
}
Output:
* * *
* * *
* * *
* * *
* * *
* * *
* * *
* * *
Explanation:
This pattern program in C combines diamond shape logic inside a square grid. It creates a grid with spaces around a centered diamond made of stars.
Code:
#include <stdio.h>
int main() {
int i, j;
int n = 5;
// Loop for upper part of diamond
for(i = 1; i <= n; i++) {
for(j = 1; j <= n - i; j++) {
printf(" "); // Print leading spaces
}
for(j = 1; j <= (2 * i - 1); j++) {
printf("* "); // Print stars
}
printf("\n"); // Move to next row
}
// Loop for lower part of diamond
for(i = n - 1; i >= 1; i--) {
for(j = 1; j <= n - i; j++) {
printf(" "); // Print leading spaces
}
for(j = 1; j <= (2 * i - 1); j++) {
printf("* "); // Print stars
}
printf("\n"); // Move to next row
}
return 0;
}
Output:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Explanation:
Pattern programs in C offer a fantastic way to strengthen your problem-solving skills and enhance your understanding of loops, arrays, and logic. By practicing different types of pattern programs in C, from simple shapes to complex spirals and numbers, you develop a deeper grasp of how to manipulate characters and spaces within the confines of a programming language.
Additionally, these patterns not only improve your coding skills but also allow you to think creatively and logically about how to achieve visually appealing outputs. They are an excellent way for beginners and intermediate programmers to sharpen their skills, boost their confidence, and get comfortable with fundamental programming concepts.
As you continue to explore more advanced pattern programs in C, you'll find that the knowledge gained from these exercises can be applied to other areas of programming, such as algorithm design, game development, and even data visualization. Whether you're preparing for coding interviews, working on academic assignments, or simply exploring the beauty of programming, mastering pattern programs in C will undoubtedly be a valuable addition to your coding toolkit.
Pattern programs in C are exercises where you write code to generate various patterns using characters, numbers, or symbols. These programs often use loops (like `for` or `while`) to print the desired pattern on the screen. They are excellent for mastering basic programming concepts like loops, arrays, and conditionals.
Pattern programs in C help you develop logical thinking and problem-solving skills. By working with different types of patterns, you learn to break down problems into smaller tasks and understand the flow of code better. This kind of practice builds a strong foundation for tackling more complex programming challenges.
Pattern programs are common in coding interviews because they test a candidate's ability to understand and implement basic programming concepts like loops, arrays, and conditionals. These exercises also reveal how well a candidate can think logically and efficiently solve problems, which is essential for more advanced technical tasks in programming.
Loops, particularly nested loops, are at the core of pattern programs in C. They allow you to print repeated characters in rows and columns. Using loops, you can control the flow of output, iterating over multiple rows and columns to create various shapes, numbers, or star patterns, which would be difficult without loops.
Yes, the logic behind pattern programs in C can be applied in real-world scenarios. For example, creating patterns can help with graphical programming, user interface design, and visual layouts in games. Understanding how to manipulate data within specific constraints, as demonstrated in pattern programs, is a skill that can be applied to many types of software development projects.
In more complex pattern programs in C, multidimensional arrays (like 2D arrays) are often used to store and print patterns. For instance, a spiral pattern or a matrix pattern requires the use of arrays to hold values in a grid. This allows you to efficiently manage and manipulate large sets of data to create intricate designs.
Absolutely! While this blog focuses on pattern programs in C, the principles behind pattern creation—like using loops and arrays—are universal and can be applied in other programming languages such as Java, Python, and C++. In fact, working with patterns in multiple languages can help you better understand the similarities and differences between them, improving your overall coding proficiency.
Recursion, though not always necessary, can be useful in more advanced pattern programs in C. For example, recursive patterns like the fractal or nested shapes require the program to call itself repeatedly, breaking the pattern into smaller components. Mastering recursion opens doors to solving more complex problems and enhances your understanding of algorithm design.
Pattern programs are a great way to practice algorithm design because they require you to think critically about how to approach a problem step by step. By creating patterns like pyramids, diamonds, or spiral patterns, you learn to break down complex problems into simple, manageable tasks—an essential skill in designing efficient algorithms for more advanced programming challenges.
In some pattern programs in C, mathematical formulas are used to determine the number of characters to print or the alignment of the pattern. For instance, formulas can help calculate the spacing between stars in a pyramid or the values of numbers in a number pyramid. These patterns provide an opportunity to integrate math into programming and refine your understanding of both.
Mastering pattern programs in C improves your programming skills and demonstrates your ability to solve problems efficiently. As you tackle increasingly complex patterns, you become more adept at logical thinking, which is a valuable skill in coding interviews, software development, and algorithm design. It sets you up for success in various programming and tech career paths.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author|900 articles published
Previous
Next
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.