C Program to Print Triangle, Pyramid, Pascal's Triangle, Floyd's Triangle, etc.

Updated on September 30, 2024
Print Triangle, Pyramid, Pascal's Triangle, Floyd's Triangle, etc. header image

Introduction

Triangles and pyramids hold particular significance in programming, especially when learning about loops and control structures in languages like C. Whether it's a simple triangle or a more complex Pascal’s Triangle, these patterns not only help in understanding nested loops but also in visualizing data structures and algorithms. These geometric patterns are commonly used in educational settings to strengthen a programmer's logic and problem-solving skills.

In this article, you will learn how to create various types of triangles and pyramids using C programming. Examples provided will include a basic triangle, a pyramid, Pascal’s Triangle, and Floyd’s Triangle. Follow along to see how to harness loops to build these structures systematically.

Printing a Simple Triangle

To print a simple triangle, use nested loops; an outer loop to handle the number of rows, and an inner loop for managing the spaces and the printing of characters (like *).

Create a Basic Triangle

  1. Define the number of rows for the triangle.

  2. Use a for loop to iterate over each row.

  3. Inside the loop, use another for loop to handle spaces, followed by a loop to handle the printing of *.

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

    This code block creates a simple triangle. The outer loop manages the rows, and the inner loops handle spaces and stars for each row respectively.

Printing a Pyramid

A pyramid is symmetrical, and hence the approach changes slightly, particularly in how spaces and characters are managed.

Create a Pyramid

  1. Set up the number of rows for the pyramid.

  2. Execute a for loop for the number of rows declared.

  3. Within the loop, manage the spaces first and then the stars, ensuring symmetry.

    c
    #include <stdio.h>
    
    void printPyramid(int rows) {
        int i, j, space;
        for(i = 1; i <= rows; i++) {
            for(space = 1; space <= (rows-i); space++) {
                printf(" ");
            }
            for(j = 1; j <= (2*i - 1); j++) {
                printf("*");
            }
            printf("\n");
        }
    }
    
    int main() {
        int rows;
        printf("Enter number of rows: ");
        scanf("%d", &rows);
    
        printPyramid(rows);
    
        return 0;
    }
    

    This function leverages double loops to manage spaces and stars, crafting a symmetric pyramid pattern based on the number of rows input by the user.

Pascal's Triangle

Pascal's Triangle showcases the concept of combinations arranged in a triangular pattern. Each number is the sum of the two directly above it.

Create Pascal's Triangle

  1. Understand that each line corresponds to the coefficients in the binomial expansion.

  2. Use two nested loops to calculate the value of each element.

  3. Remember that the number at each position is calculated using the formula nCr = n! / (r! * (n-r)!).

    c
    #include <stdio.h>
    
    int binomialCoeff(int n, int k) {
        int res = 1;
        if (k > n - k)
            k = n - k;
        for (int i = 0; i < k; ++i) {
            res *= (n - i);
            res /= (i + 1);
        }
        return res;
    }
    
    void printPascalsTriangle(int rows) {
        for (int line = 0; line < rows; line++) {
            for (int i = 0; i <= (rows - line - 1); i++) {
                printf(" ");
            }
            for (int i = 0; i <= line; i++)
                printf("%d ", binomialCoeff(line, i));
            printf("\n");
        }
    }
    
    int main() {
        int rows;
        printf("Enter the number of rows: ");
        scanf("%d", &rows);
        printPascalsTriangle(rows);
        return 0;
    }
    

    This implementation uses a helper function, binomialCoeff, to calculate the coefficients. The main function then formats these coefficients into the shape of Pascal's Triangle.

Conclusion

Building triangles and pyramids in C, from simple to complex structures like Pascal's Triangle, not only enhances your grasp of nested loops but also improves your understanding of mathematical concepts applied in programming. By following the provided examples, you can create visually appealing and intricate patterns that bolster both your coding and problem-solving skills in C. Utilize these techniques in various applications, adjusting and expanding upon them as needed to fit different scenarios and requirements.