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

</>
Copy
#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:

  1. We declare an integer variable rows and set it to 5, which defines the number of rows in the pattern.
  2. The outer for loop runs from 1 to rows, controlling the number of rows printed.
  3. The inner for loop runs from 1 to i and prints a star (*) in each iteration.
  4. 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

</>
Copy
#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:

  1. We declare an integer variable size and set it to 5, which defines the dimensions of the square.
  2. The outer for loop runs from 1 to size, controlling the number of rows.
  3. The inner for loop runs from 1 to size, printing * in each column.
  4. 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

</>
Copy
#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:

  1. We declare an integer variable rows and set it to 5, defining the pyramid’s height.
  2. The outer loop iterates from 1 to rows, controlling the number of rows.
  3. The first inner loop prints spaces (" ") to align the stars correctly.
  4. The second inner loop prints stars, where the number of stars is determined by 2 * i - 1 to form a pyramid.
  5. 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:

  1. Right-Angled Triangle: Using nested loops to print increasing stars.
  2. Square Pattern: Using a fixed number of stars in each row and column.
  3. Pyramid Pattern: Using spaces and stars to create a symmetric shape.