C Program to Print Floyd's Triangle

This article will teach you how to print Floyd's triangle in C and provide you with code to do so. But before going through the program, let's first understand Floyd's Triangle.

What is Floyd's Triangle?

Floyd's triangle is a right-angled triangle created using natural numbers, as shown here:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

may continue up to any number of rows. Here, Floyd's triangle is shown up to 5 rows only.

Print Floyd's Triangle in C

Now let's create a program to print Floyd's triangle with 5 lines or rows:

#include<stdio.h>
#include<conio.h>
int main()
{
    int i, j, num=1;
    for(i=0; i<5; i++)
    {
        for(j=0; j<=i; j++)
        {
            printf("%d ", num);
            num++;
        }
        printf("\n");
    }
    getch();
    return 0;
}

This program was built and runs in the Code::Blocks IDE. Here is its sample run:

c program print floyd triangle

The dry run of the above program goes like this:

Allow the user to define the size of Floyd's triangle

Here is another program that does the same job as the previous one, which is to print Floyd's triangle. But the user is allowed to define the size of Floyd's triangle. In other words, the program asks the user how many lines Floyd's triangle should expand to.

#include<stdio.h>
#include<conio.h>
int main()
{
    int i, j, num=1, row;
    printf("Enter Number of Rows: ");
    scanf("%d", &row);
    for(i=0; i<row; i++)
    {
        for(j=0; j<=i; j++)
        {
            printf("%d ", num);
            num++;
        }
        printf("\n");
    }
    getch();
    return 0;
}

Here is its sample run, supposing the user input is 10:

print floyd triangle c

The same program in different languages

C Quiz


« Previous Program Next Program »


Liked this post? Share it!