🌟 Pattern Programs in C: A Guide to Mastering Number Patterns Part - Three 🌟

🌟 Pattern Programs in C: A Guide to Mastering Number Patterns Part - Three 🌟

Anshuman Champatiray
Anshuman Champatiray

Share it on

Five Pattern Programs in C πŸŽ‰

Patterns are a great way to understand looping and nested structures in programming. Here, we’ll explore five different pattern programs in C, each demonstrating various ways of structuring loops. These examples will help you grasp key concepts of nested loops, conditional logic, and output formatting in C. Let’s get started! πŸš€


1. Right-Aligned Number Pyramid πŸŽ‡

This program creates a right-aligned number pyramid. The numbers increase from 1 to the row number in each row, aligning to the right.

#include <stdio.h>
int main() {
    int i, j, l, n;
    printf("Enter a number : ");
    scanf("%d", &n);
    for (i = 1; i <= n; i++) {
        for (l = 1; l <= n - i; l++)
            printf(" ");
        for (j = 1; j <= i; j++)
            printf("%d", j);
        printf("\n");
    }
    return 0;
}

Explanation

  • The outer loop (i) represents each row.
  • The first inner loop (l) handles spaces to right-align the numbers.
  • The second inner loop (j) prints numbers from 1 to i.

Example Output

For n = 5, the output will be:

    1
   12
  123
 1234
12345

2. Right-Aligned Inverted Pyramid ⬇️

This program creates a right-aligned inverted number pattern starting from the input number n and decreasing each row.

#include <stdio.h>
int main() {
    int i, j, l, n;
    printf("Enter a number : ");
    scanf("%d", &n);
    for (i = n; i >= 1; i--) {
        for (l = 1; l <= i - 1; l++)
            printf(" ");
        for (j = i; j <= n; j++)
            printf("%d", j);
        printf("\n");
    }
    return 0;
}

Explanation

  • The outer loop (i) decreases with each row, starting from n.
  • The first inner loop (l) manages spaces to right-align the numbers.
  • The second inner loop (j) prints numbers from i to n.

Example Output

For n = 5, the output will be:

    5
   45
  345
 2345
12345

3. Left-Aligned Descending Pyramid πŸ”»

In this pattern, numbers start from the current row number and decrease until reaching the base of the pyramid.

#include <stdio.h>
int main() {
    int i, j, l, n;
    printf("Enter a number : ");
    scanf("%d", &n);
    for (i = 1; i <= n; i++) {
        for (l = 1; l <= i - 1; l++)
            printf(" ");
        for (j = i; j <= n; j++)
            printf("%d", j);
        printf("\n");
    }
    return 0;
}

Explanation

  • The outer loop (i) iterates for each row.
  • The first inner loop (l) controls spaces at the beginning of each row.
  • The second inner loop (j) prints numbers from i to n, shifting left each row.

Example Output

For n = 5, the output will be:

12345
 2345
  345
   45
    5

4. Left-Aligned Inverted Pyramid πŸ”Ό

This program produces a left-aligned inverted pyramid where each row begins with 1 and the number count decreases until n reaches 1.

#include <stdio.h>
int main() {
    int i, j, l, n;
    printf("Enter a number : ");
    scanf("%d", &n);
    for (i = n; i >= 1; i--) {
        for (l = 1; l <= n - i; l++)
            printf(" ");
        for (j = 1; j <= i; j++)
            printf("%d", j);
        printf("\n");
    }
    return 0;
}

Explanation

  • The outer loop (i) decreases row by row.
  • The first inner loop (l) manages spaces to align the numbers to the left.
  • The second inner loop (j) prints numbers from 1 to i.

Example Output

For n = 5, the output will be:

12345
 1234
  123
   12
    1

5. Center-Aligned 1 Pyramid 🌟

In this pattern, the program creates a centered pyramid of 1s, with spaces between each 1.

#include <stdio.h>
int main() {
    int i, j, l, n;
    printf("Enter a number : ");
    scanf("%d", &n);
    for (i = 1; i <= n; i++) {
        for (l = 1; l <= n - i; l++)
            printf(" ");
        for (j = 1; j <= i; j++)
            printf("1 ");
        printf("\n");
    }
    return 0;
}

Explanation

  • The outer loop (i) represents rows, with the total number of rows equal to n.
  • The first inner loop (l) creates spaces for centering the 1s.
  • The second inner loop (j) prints 1 with a space in between for each column.

Example Output

For n = 5, the output will be:

    1 
   1 1 
  1 1 1 
 1 1 1 1 
1 1 1 1 1 

These patterns provide insights into using nested loops, alignment with spaces, and the logic for generating pyramids and inverted pyramids. Try experimenting with different values of n to see how the patterns change! 🧩


Happy Coding! 🎈

More Suggested Blogs