View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

Pattern Program in C: A Complete Guide

Updated on 22/04/20258,166 Views

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. 

What Are Pattern Programs in C?

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).

Applications of Pattern Programs in C

Here are some of the applications of pattern programs in C:

  • Learning Loops: They are excellent for mastering for, while, and do-while loops.
  • Logic Building: Helps in improving logical thinking and problem-solving.
  • Interview Questions: Frequently asked in job interviews and coding challenges.
  • Teaching Tool: Widely used by instructors to explain control flow and nested structures.
  • Fun Projects: Great for building small, satisfying programs with visual appeal.

Now, let’s look at different types of pattern programs in C, categorized for clarity.

Type of Pattern Program in C 

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: 

Star Pattern Programs in C

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.

a. Right-Angled Triangle Star Pattern

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:

  • The outer loop runs from 1 to 5 (rows).
  • The inner loop prints `i` stars for each row `i`.
  • A newline is printed after each row to create a triangle shape.

b. Inverted Right-Angled Triangle Star Pattern

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:

  • The outer loop starts at 5 and goes down to 1.
  • Inner loop prints stars equal to the current value of `i`.
  • Each row prints fewer stars than the previous one.

Also explore Post Graduate Diploma in Management for a high-paying career in the long run

c. Hollow Square Star Pattern

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:

  • Two nested loops traverse rows and columns.
  • A star is printed only when the current cell is on the first or last row/column.
  • Otherwise, spaces are printed to create the hollow center.

d. Pyramid Star Pattern

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:

  • The outer loop controls rows.
  • First inner loop prints leading spaces to center-align the stars.
  • Second inner loop prints an odd number of stars per row (`2*i - 1`).
  • Result: a symmetrical pyramid pattern.

Must explore Artificial Intelligence & Machine Learning course to fast-forward your career to a high-paying position. 

Number Pattern Programs in C

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.

a. Floyd’s Triangle

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:

2 3 

4 5 6 

7 8 9 10 

11 12 13 14 15 

Explanation:

  • A single counter `num` keeps incrementing across all rows.
  • Outer loop controls the number of rows.
  • Inner loop prints the increasing numbers in each row.

b. Number Pyramid Pattern

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:

  • Outer loop controls rows.
  • First inner loop prints spaces for alignment.
  • Second inner loop prints numbers from 1 to the current row number.

c. Palindromic Number Pattern

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:

  • Each row starts with spaces for center alignment.
  • Then numbers decrease from `i` to 1.
  • Followed by numbers increasing from 2 to `i`, forming a palindromic shape.

d. Pascal’s Triangle

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:

  • Uses `nCr = n! / (r! * (n-r)!)` to generate each value.
  • Numbers are aligned symmetrically in a triangle.
  • Great for combining math with C programming logic. 

Alphabet Pattern Programs in C

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:

a. Alphabet Pyramid Pattern

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 B 

A B C 

A B C D 

A B C D E 

Explanation:

  • Each row prints characters starting from `'A'` up to that row's length.
  • Characters are incremented using `ch++`. 

b. Inverted Alphabet Triangle

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 

Explanation:

  • Outer loop starts at 5 and decreases.
  • Each row prints fewer characters from 'A'.

c. Alphabet Triangle with Increasing Starting Letters

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:

B C 

C D E 

D E F G 

E F G H I 

Explanation:

  • Each row starts with a different character.
  • Characters are incremented row-wise using `start = 'A' + i - 1`.

d. Reverse Alphabet Triangle

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 

Explanation:

  • Characters start from `'E'` and decrement.
  • Row length decreases as `i` increases.

Grid and Square Pattern Programs in C

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:

a. Solid Square Pattern

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:

  • Both loops run from 1 to the `size` of the square.
  • The inner loop prints stars (`*`), while the outer loop controls the number of rows.
  • This results in a full square of asterisks.

b. Hollow Square Pattern

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 outer loop iterates over rows, and the inner loop iterates over columns.
  • A conditional statement checks if the current position is on the border of the square (first row, last row, first column, or last column).
  • The program prints stars on the borders and spaces inside the square. 

c. Checkerboard Pattern

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:

  • The outer loop controls rows and the inner loop controls columns.
  • The condition `(i + j) % 2 == 0` ensures that stars (`*`) and spaces alternate in a checkerboard pattern.
  • Even positions (sum of row and column numbers) get a star, while odd positions get spaces.

d. Diamond Pattern in a Square

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:

  • The first loop prints the upper part of the diamond, starting with one star and increasing by 2 each row.
  • The second loop prints the lower part of the diamond, decreasing the stars row by row.
  • Leading spaces are used to center-align the stars in both halves. 

Conclusion

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. 

FAQs

1. What are pattern programs in C?  

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.

2. How do pattern programs help in improving programming skills?  

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.

3. Why are pattern programs often used in coding interviews?  

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.

4. What is the significance of loops in pattern programs in C?  

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.

5. Can pattern programs be applied in real-world programming?  

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.

6. How do multidimensional arrays play a role in pattern programs?  

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.

7. Can I create pattern programs using other programming languages?

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.

8. What is the importance of recursion in pattern programs in C?  

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.

9. How can pattern programs be useful in 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.

10. What role do mathematical formulas play in pattern programs?  

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.

11. How can mastering pattern programs in C benefit my career?

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. 

image

Take a Free C Programming Quiz

Answer quick questions and assess your C programming knowledge

right-top-arrow
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
advertise-arrow

Free Courses

Start Learning For Free

Explore Our Free Software Tutorials and Elevate your Career.

upGrad Learner Support

Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918068792934

Disclaimer

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.