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

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

Anshuman Champatiray
Anshuman Champatiray

Share it on

Mastering Patterns in C: Five Engaging Programs

C programming offers numerous possibilities for creating interesting patterns using loops. In this blog post, we’ll explore five unique pattern programs, each designed to demonstrate different looping techniques and logic. We’ll provide explanations, code snippets, and example outputs for each program. Let’s dive in!

Program 1: Ascending Number Pyramid

Code

#include<stdio.h>

int main() {
    int i, j, l, k, n;
    printf("Enter a number: ");
    scanf("%d", &n);
    
    for (i = 1; i <= n; i++) {
        l = 1;
        k = i;
        
        for (j = 1; j <= 2*n - 1; j++) {
            if (l <= i)
                printf("%d", l);
            else
                printf(" ");
                
            if (j < n)
                l++;
            else
                l--;
        }
        printf("\n");
    }

    return 0;
}

Explanation

In this program, we create a pyramid of numbers. The outer loop runs from 1 to n, which is the height of the pyramid. For each row, we print numbers in ascending order until we reach the midpoint, then decrement. The inner loop handles spacing to maintain the pyramid shape.

Example Output

For an input of 5:

1       1
12     21
123   321
1234 4321
1234554321

Program 2: Number Triangle

Code

#include<stdio.h>

int main() {
    int i, j, l, n;
    printf("Enter a number: ");
    scanf("%d", &n);
    
    l = 1;
    for (i = 1; i <= 2*n - 1; i++) {
        for (j = 1; j <= l; j++)
            printf("%d", j);

        if (i < n)
            l++;
        else
            l--;

        printf("\n");
    }
    
    return 0;
}

Explanation

This program generates a triangle of numbers where the number of digits increases to n and then decreases. The outer loop controls the number of lines, while the inner loop prints the numbers.

Example Output

For an input of 5:

1
12
123
1234
12345
1234
123
12
1

Program 3: Inverted Number Pyramid

Code

#include<stdio.h>

int main() {
    int i, j, k, l, n;
    printf("Enter a number: ");
    scanf("%d", &n);
    
    l = 0;
    for (i = 1; i <= 2*n - 1; i++) {
        for (k = 1; k <= l; k++)
            printf(" ");

        for (j = 1; j <= n - l; j++)
            printf("%d", j);

        if (i < n)
            l++;
        else
            l--;

        printf("\n");
    }

    return 0;
}

Explanation

This program creates an inverted pyramid of numbers. The outer loop manages the rows, while the inner loops handle the spaces and the decreasing numbers.

Example Output

For an input of 5:

12345
 1234
  123
   12
    1
   12
  123
 1234
12345

Program 4: Diamond Number Pattern

Code

#include<stdio.h>

int main() {
    int i, j, k, l, m, n;
    printf("Enter a number: ");
    scanf("%d", &n);
    
    l = 1;
    for (i = 1; i <= 2*n - 1; i++) {
        for (k = 1; k <= n - l; k++)
            printf(" ");
            
        m = 1;
        for (j = 1; j <= 2*l - 1; j++) {
            printf("%d", m);

            if (j < l)
                m++;
            else
                m--;
        }
        if (i < n)
            l++;
        else
            l--;

        printf("\n");
    }

    return 0;
}

Explanation

This program creates a diamond shape with numbers, where the width of the diamond increases until it reaches the middle row and then decreases. The logic involves printing spaces and managing the number of printed numbers per row.

Example Output

For an input of 5:

    1
   121
  12321
 1234321
123454321
 1234321
  121
   1

Program 5: Alphabet Diamond Pattern

Code

#include<stdio.h>

int main() {
    int i, j, k, l, n;
    printf("Enter a number: ");
    scanf("%d", &n);
    
    for (i = 1; i <= n; i++) {
        for (k = 1; k <= n - i; k++)
            printf(" ");
            
        l = 65;
        for (j = 1; j <= 2*i - 1; j++) {
            printf("%c", l);

            if (j < i)
                l++;
            else
                l--;
        }

        printf("\n");
    }

    return 0;
}

Explanation

This program generates a diamond pattern using uppercase letters. Similar to the previous diamond, it increases the number of characters in the first half and decreases in the second half. The ASCII value of A (65) is used to print characters.

Example Output

For an input of 5:

    A
   ABA
  ABCBA
 ABCDCBA
ABCDEDCBA

Conclusion

These five C programs demonstrate the versatility of loops in creating different patterns. By understanding and modifying these examples, you can develop your skills further and even create your own unique patterns. Happy coding!

More Suggested Blogs