1.
Introduction
2.
What is Floyd's triangle in C?
2.1.
Example
2.1.1.
Explanation
3.
Algorithm
4.
Program to print the Floyd's triangle using for loop
4.1.
C
5.
Program to print the Floyd's triangle using while loop
5.1.
C
6.
Program to print the Floyd's triangle using the Recursion function
6.1.
C
7.
Program to reverse the Floyd's triangle using for loop
7.1.
C
8.
â€‹â€‹Program to print the star Floyd's triangle using for loop
8.1.
C
9.
Program to print the Alphabets Floyd's triangle in C using for loop
9.1.
C
10.
10.1.
What is the Floyd number pattern in C?
10.2.
Why is this triangle pattern given a name as  Floyd's triangle?
10.3.
What is the formula for Floyd's triangle?
10.4.
What is the use of printf and scanf in C?
10.5.
What is the real life application of Floyd's triangle?
11.
Conclusion
Last Updated: Mar 27, 2024
Easy

# Floyd's Triangle in C

Apoorv
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

## Introduction

Floyd's Triangle is a right-angled triangle consisting of an array of natural numbers named after its founder Robert W. Floyd.

It is formed by starting with the number 1 and printing a row of one number, followed by incrementing the number of rows until the desired number of rows gets printed. Each row starts with the following consecutive number after the previous row.

## What is Floyd's triangle in C?

A right-angled triangle with successive natural integers is known as Floyd's triangle. The number in Floyd's triangle starts with 1 in the top left corner and continues to fill the defined rows with numbers. In this blog, we will further work on the implementation of Floyd's triangle in c.

### Example

If we have to print 6 rows of Floyd's triangle it will be in the following way:

1

2 3

4 5 6

7 8 9 10

11 12 13 14 15

16 17 18 19 20 21

#### Explanation

Print the normal counting of numbers and in each row, print a count of a number equal to the row number for example, in row 1, print 1 element, then in row 2, print 2 elements, and so on.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## Algorithm

• Create the variables for holding row number and column number as i and j. Then further take a number to display the rows as 'row' and set the variable count to 1 which will print the elements.
• Now Use the concept of nested loops.
• The iteration of the outer for loop begins with I = 1 and continues up to total rows.
• Iteration of the inner for loop begins at j = 1 and ends at (j=i).
• Print the count values.
• Increase count by one or make count = count + 1.
• After each iteration of the inner for loop, jump to a new line.
• Stop

## Program to print the Floyd's triangle using for loop

• C

### C

``#include <stdio.h>  void main()  {      int n,i,j,k = 1;          printf( "Number of rows: ");      scanf( "%d", &n);      // Outer for loop to define the rows.        for (i = 1; i <= n; i++)      {          // Inner for loop for checking the value of j.                for (j = 1; j <= i; j++)          {              printf(" %2d ", k++);         }          printf( "\n");      }      return ;}  ``

Output

``````Number of rows: 5
1
2   3
4   5   6
7   8   9  10
11  12  13  14  15 ``````

In the above example, we have to print 5 rows of Floydsâ€™s Triangle. For that, we use a nested for loop to print the triangle.

The first or the outer for loop will count using the variable as 1 and will check the condition where i <= n. The second or the inner for loop works through  j= 1 till j = i.

## Program to print the Floyd's triangle using while loop

• C

### C

``#include<stdio.h>    void main()  {      int n, i = 1, j = 1, k;        printf("Number of rows: ");      scanf("%d", &n);        printf("\n");        while(j <= n)      {          k = 1;          while(k <= j)          {              printf("%d  ", i);              i++;              k++;          }          j++;          printf("\n");      }        return 0;  }  ``

Output

``````Number of rows: 5
1
2   3
4   5   6
7   8   9  10
11  12  13  14  15 ``````

In this program we printed 5 rows of Floydâ€™s Triangle using nested while loops.

## Program to print the Floyd's triangle using the Recursion function

• C

### C

``#include <stdio.h>  // Declaring of global variables  int rows = 1;  int k = 1;    // Defining the function  void Floyd_triangle (int n) {      int i;      if (n <= 0)           return;            for ( i = 1; i <= rows; i++)                printf( " %d", k++);                    printf ("\n");          rows++;          Floyd_triangle (n - 1);  }  int main()  {      int n;     printf( " Number of rows: ");      scanf (" %d", &n);          // Calling  Floyd_triangle function         Floyd_triangle( n);      return 0;         }``

Output

``````Number of rows: 6
1
2   3
4   5   6
7   8   9  10
11  12  13  14  15
16  17  18  19  20  21 ``````

In this program, we printed 6 rows of Floydâ€™s Triangle using the recursion function and for loop to define the rows.

## Program to reverse the Floyd's triangle using for loop

• C

### C

``#include <stdio.h>  int main()  {      int n, i, j, k;      printf ("Number of rows: ");      scanf (" %d", & n);             //Formula to get the reverse Floyd's triangle         k = n * ( n + 1) / 2;            //Outer loop          for (i = n; i >= 0; i--)      {          //Checking the i condition                   for (j = 1; j <= i; j++)          {                         printf (" %d", k--);           }          printf ("\n");      }      return 0;      }  ``

Output

``````Number of rows: 7
28 27 26 25 24 23 22
21 20 19 18 17 16
15 14 13 12 11
10 9 8 7
6 5 4
3 2
1``````

In this program, we formed Floydâ€™s Triangle in a reversed order using nested for loops.
Must Read  C Program to Reverse a Number

## â€‹â€‹Program to print the star Floyd's triangle using for loop

• C

### C

``#include <stdio.h> void main() { int n, i, j, k = 1;    printf( "Number of rows: "); scanf( "%d", &n); // outer for loop define the rows and check rows condition for (i = 1; i <= n; i++) {      // inner loop check j should be less than equal to 1 and print the data.           for (j = 1; j <= i; j++)      {             printf(" * ");               }      printf( "\n");      }  return ;  }  ``

Output

``````Number of rows: 8
*
*  *
*  *  *
*  *  *  *
*  *  *  *  *
*  *  *  *  *  *
*  *  *  *  *  *  *
*  *  *  *  *  *  *  * ``````

In this program, we made a Floydâ€™s Triangle using stars. We used nested for loops where the first or outer for loop will count using the variable as 1 and will check the condition where i <= n. The second or the inner for loop works through  j= 1 till j = i.

Must Read Passing Arrays to Function in C

## Program to print the Alphabets Floyd's triangle in C using for loop

• C

### C

``#include <stdio.h>  void main()  {      int n, i, j, k = 'A';          printf( "Number of rows: ");      scanf( "%d", &n);        for (i = 1; i <= n; i++)      {               for (j = 1; j <= i; j++)          {              printf(" %c", k);             k++;          }          printf( "\n");          }     return 0;   }  ``

Output

``````Number of rows: 5
A
B C
D E F
G H I J
K L M N O``````

In this program, we made a Floydâ€™s Triangle using alphabets. We used nested for loops where the first or outer for loop will count using the variable as 1 and will check the condition where i <= n. The second or the inner for loop works through  j= 1 till j = I.

Also see,  Short int in C Programming

### What is the Floyd number pattern in C?

Floyd's number pattern is an arrangement of numbers in a right-angled triangle. It starts with 1 at the top-left corner and increments consecutively as you move from left to right and top to bottom. It can be generated using nested loops in C.

### Why is this triangle pattern given a name as  Floyd's triangle?

It was named after a computer scientist Robert w. Floyd.

### What is the formula for Floyd's triangle?

Floyd's triangle is created by filling rows with consecutive numbers, starting with 1. Each row has one more number than the previous row, forming a triangular pattern of numbers. There's no specific formula, but a pattern of increasing numbers.

### What is the use of printf and scanf in C?

We have used printf to print the elements of Floyd's triangle in C printf is used to print a statement in C and scanf is used to taking the user input in C.scanf in the implementation is used to taking the user input for the number of rows. Here the number of rows will signify the number of rows in Floyd's triangle in C.

### What is the real life application of Floyd's triangle?

To a mathematician, it isn't really relevant. Its primary purpose is to serve as a beginner's computer programming exercise. Aside from that, it's a good example of what's known as "triangular numbers."

## Conclusion

In this article, we have extensively discussed the solution to print Floyd's Triangle in C along with its time and space complexity.

Until then, All the best for your future endeavors, and Keep Coding. "We hope that this blog has helped you enhance your knowledge regarding this problem, and if you would like to learn more, check out our articles on  Coding Ninjas Studio. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass