The Floyd's triangle is a right-angled triangular array of natural numbers, used in computer science education. The triangle is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner: 1. 2. Successive rows start towards the left with the next number followed by successive naturals listing one more number than the line above. This exercise is often necessary while you learn to code in the School or University.

In this article, we'll show you briefly how to print floyd's triangle in the console with C.

## Implementation

To get started with the C logic, you will need to loop twice and create a counter variable that in this case will be `a`

. In the first for loop you will iterate the number of times of the given number by the user, inside this loop you will iterate as well the number of times of the index of the first loop, incrementing the value of the `a`

counter by one, inside this loop you will print that variable that contains the consecutive number of the Floyd's triangle:

```
#include <stdio.h>
int main()
{
int n, i, c, a = 1;
printf("Enter the number of rows of Floyd's triangle to print: \n");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (c = 1; c <= i; c++)
{
printf("%d ",a);
a++;
}
printf("\n");
}
return 0;
}
```

Compiling the previous code will generate a console that will prompt the user for a number that defines the number of rows of the triangle as shown in the image of this article.

## Good to know

In math class, you may find a problem where your teacher will ask what's the last number of a given row in the floyd's triangle. With math calculations, you can find the last number of the floyd's triangle knowing the number of rows "`n`

" with the following formula:

For example given the following triangle:

```
1
2 3
4 5 6
7 8 9 10
11 12 13 14
15 16 17 18 19
.....
```

Which would be the last number of the 10th row? Just replacing values in the mentioned the formula, we would replace the value `n`

with the number of the row to know which number would appear as last in the mentioned row:

`(10 * (10 + 1)) / 2 = 55`

If we print it with our code, the output will be:

```
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
```

We will know that the last number of a floyd's triangle with 10 rows will be 55, so the formula works as expected.

Happy coding !

## Become a more social person