Open In App

C Program to Print Floyd’s Triangle Pyramid Patterns

Last Updated : 15 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Floyd’s triangle pyramid pattern is a pattern where numbers or characters are printed in a triangular shape, with each row containing an increasing number of consecutive integers or characters aligned to the left forming a shape similar to right half pyramid pattern.

Floyds-Triangle-Pyramid-Patterns__

Floyd’s Triangle Pyramid Patterns

In this article, we will learn how to print Floyd’s triangle pyramid patterns using a C program. Let’s take a look at the code implementation:

Number Pattern
#include <stdio.h>

int main() {
    int n = 5;
    int c = 1;

    // Outer loop to print all rows
    for (int i = 0; i < n; i++) {

        // Inner loop to print number in each row
        for (int j = 0; j <= i; j++) {
            printf("%d ", c++);
        }
        printf("\n");
    }
    return 0;
}
Alphabet Pattern
#include <stdio.h>

int main() {
    int n = 5;
    char c = 'A';

    // Outer loop to print all rows
    for (int i = 0; i < n; i++) {

        // Inner loop to print alphabet in each row
        for (int j = 0; j <= i; j++) {
            printf("%c ", c++);
        }
        printf("\n");
    }
    return 0;
}


Output

1          |   A
2 3 | B C
4 5 6 | D E F
7 8 9 10 | G H I J

Explanation: In this the outer loop controls the number of rows, while the inner loop prints the numbers in each row. The numbers are printed sequentially, starting from 1 and incrementing with each position. After each row is printed, a newline character is added to move to the next row, creating the triangular shape. The pattern expands row by row, with each row containing one more number than the previous one, forming a pyramid-like structure.



Next Article

Similar Reads