C Programming Code To Create Pyramid and Pattern

Created with Sketch.

Patterns in C: Triangles, Pyramids, and Pascal’s Triangle

Introduction

In this blog post, we’ll explore various patterns in C programming, including printing triangles using asterisks and digits, inverted triangles, full pyramids, Pascal’s triangle, and Floyd’s triangle. We’ll provide C code examples for each pattern, along with outputs and detailed explanations of the algorithms.

1. Printing Triangles Using *

Algorithm

  1. Take an input for the number of rows n.
  2. Use nested loops to iterate over rows and columns.
  3. In the inner loop, print asterisks (*) based on the current row and column.

C Code Example

#include <stdio.h>

void printAsteriskTriangle(int n) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }
}

int main() {
    int rows;

    // Input: Get the number of rows from the user
    printf("Enter the number of rows for the asterisk triangle: ");
    scanf("%d", &rows);

    // Print asterisk triangle
    printAsteriskTriangle(rows);

    return 0;
}

Output Example

Input: 5

Enter the number of rows for the asterisk triangle: 5
*
* *
* * *
* * * *
* * * * *

2. Printing Inverted Triangles Using *

Algorithm

  1. Take an input for the number of rows n.
  2. Use nested loops to iterate over rows and columns.
  3. In the inner loop, print asterisks (*) based on the current row and column.

C Code Example

#include <stdio.h>

void printInvertedAsteriskTriangle(int n) {
    for (int i = n; i >= 1; i--) {
        for (int j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }
}

int main() {
    int rows;

    // Input: Get the number of rows from the user
    printf("Enter the number of rows for the inverted asterisk triangle: ");
    scanf("%d", &rows);

    // Print inverted asterisk triangle
    printInvertedAsteriskTriangle(rows);

    return 0;
}

Output Example

Input: 4

Enter the number of rows for the inverted asterisk triangle: 4
* * * *
* * *
* *
*

3. Printing Full Pyramids

Algorithm

  1. Take an input for the number of rows n.
  2. Use nested loops to iterate over rows and columns.
  3. In the inner loop, print spaces and asterisks based on the current row and column.

C Code Example

#include <stdio.h>

void printFullPyramid(int n) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i; j++) {
            printf("  ");
        }
        for (int k = 1; k <= 2 * i - 1; k++) {
            printf("* ");
        }
        printf("\n");
    }
}

int main() {
    int rows;

    // Input: Get the number of rows from the user
    printf("Enter the number of rows for the full pyramid: ");
    scanf("%d", &rows);

    // Print full pyramid
    printFullPyramid(rows);

    return 0;
}

Output Example

Input: 3

Enter the number of rows for the full pyramid: 3
    *
  * * *
* * * * *

4. Printing Pascal’s Triangle

Algorithm

  1. Take an input for the number of rows n.
  2. Use nested loops to iterate over rows and columns.
  3. Calculate and print the binomial coefficient for each position.

C Code Example

#include <stdio.h>

// Function to calculate factorial
int factorial(int num) {
    if (num == 0 || num == 1) {
        return 1;
    } else {
        return num * factorial(num - 1);
    }
}

// Function to calculate binomial coefficient
int binomialCoefficient(int n, int r) {
    return factorial(n) / (factorial(r) * factorial(n - r));
}

void printPascalsTriangle(int n) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j <= i; j++) {
            printf("%d ", binomialCoefficient(i, j));
        }
        printf("\n");
    }
}

int main() {
    int rows;

    // Input: Get the number of rows from the user
    printf("Enter the number of rows for Pascal's triangle: ");
    scanf("%d", &rows);

    // Print Pascal's triangle
    printPascalsTriangle(rows);

    return 0;
}

Output Example

Input: 5

Enter the number of rows for Pascal's triangle: 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

5. Printing Floyd’s Triangle

Algorithm

  1. Take an input for the number of rows n.
  2. Use a counter variable to print numbers in ascending order.

C Code Example

#include <stdio.h>

void printFloydsTriangle(int n) {
    int counter = 1;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            printf("%d ", counter);
            counter++;
        }
        printf("\n");
    }
}

int main() {
    int rows;

    // Input: Get the number of rows from the user
    printf("Enter the number of rows for Floyd's triangle: ");
    scanf("%d", &rows);

    // Print Floyd's triangle
    printFloydsTriangle(rows);

    return 0;
}

Output Example

Input: 4

Enter the number of rows for Floyd's triangle: 4
1
2 3
4 5 6
7 8 9 10

Conclusion

In this blog post, we explored various patterns in C programming, including triangles using asterisks, inverted triangles, full pyramids, Pascal’s triangle, and Floyd’s triangle. Each section provided a detailed explanation of the algorithm, C code examples, and output demonstrations.

Understanding these patterns is crucial for developing programming logic and enhancing problem-solving skills. Feel free to use the provided C code as a reference or incorporate it into your projects. Experiment with different inputs to observe how the patterns change based on the number of rows.

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *