Print Patterns using Loops in C
In C, we can print different patterns using loops by controlling the number of iterations and printing characters in a structured format. By using for
loops, we can create simple to complex patterns such as triangles, squares, pyramids, and more.
In this tutorial, we will go through multiple examples to understand how to generate patterns using loops.
Examples of Printing Patterns in C
1. Printing a Right-Angled Triangle Pattern
In this example, we will use Nested For loops to print a right-angled triangle of stars (*
), where each row contains an increasing number of stars.
main.c
#include <stdio.h>
int main() {
int rows = 5;
// Outer loop for number of rows
for (int i = 1; i <= rows; i++) {
// Inner loop for printing stars
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Explanation:
- We declare an integer variable
rows
and set it to5
, which defines the number of rows in the pattern. - The outer
for
loop runs from1
torows
, controlling the number of rows printed. - The inner
for
loop runs from1
toi
and prints a star (*
) in each iteration. - After printing the stars for a row, we use
printf("\n")
to move to the next line.
Output:
*
* *
* * *
* * * *
* * * * *
2. Printing a Square Pattern
In this example, we will print a square pattern using For loops where each row contains the same number of stars.
main.c
#include <stdio.h>
int main() {
int size = 5;
// Outer loop for rows
for (int i = 1; i <= size; i++) {
// Inner loop for columns
for (int j = 1; j <= size; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Explanation:
- We declare an integer variable
size
and set it to5
, which defines the dimensions of the square. - The outer
for
loop runs from1
tosize
, controlling the number of rows. - The inner
for
loop runs from1
tosize
, printing*
in each column. - After printing all stars in a row, we use
printf("\n")
to move to the next row.
Output:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
3. Printing a Pyramid Pattern
In this example, we will print a pyramid pattern using spaces and stars to form a symmetric shape.
main.c
#include <stdio.h>
int main() {
int rows = 5;
// Outer loop for rows
for (int i = 1; i <= rows; i++) {
// Print spaces for alignment
for (int j = 1; j <= rows - i; j++) {
printf(" ");
}
// Print stars
for (int k = 1; k <= 2 * i - 1; k++) {
printf("*");
}
printf("\n");
}
return 0;
}
Explanation:
- We declare an integer variable
rows
and set it to5
, defining the pyramid’s height. - The outer loop iterates from
1
torows
, controlling the number of rows. - The first inner loop prints spaces (
" "
) to align the stars correctly. - The second inner loop prints stars, where the number of stars is determined by
2 * i - 1
to form a pyramid. - After printing a row,
printf("\n")
moves to the next line.
Output:
*
***
*****
*******
*********
Conclusion
In this tutorial, we explored different ways to print patterns using loops in C:
- Right-Angled Triangle: Using nested loops to print increasing stars.
- Square Pattern: Using a fixed number of stars in each row and column.
- Pyramid Pattern: Using spaces and stars to create a symmetric shape.