Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Floyd's triangle in C?
2.1.
Example for Floyd's triangle in C 
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.
Frequently Asked Questions
10.1.
What are the conditions for Floyd's triangle?
10.2.
What is the Floyd's triangle problem?
10.3.
How to print a Floyd triangle?
11.
Conclusion
Last Updated: Aug 25, 2024
Easy

Floyd's Triangle in C

Author Apoorv
0 upvote

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. 

Let us learn more about Floyd's Triangle in C.

 

floyd's triangle in c

 

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 for Floyd's triangle in C 

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.

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 

Frequently Asked Questions

What are the conditions for Floyd's triangle?

Floyd's Triangle requires printing integers in a triangular pattern, where each row contains sequential integers starting from 1, with the number of integers increasing per row.

What is the Floyd's triangle problem?

The Floyd's Triangle problem involves generating a right-angled triangle of numbers where each row starts with the next integer in sequence, forming a triangular pattern with consecutive numbers.

How to print a Floyd triangle?

To print Floyd's Triangle, we can use nested loops: the outer loop for rows and the inner loop for printing numbers in each row, incrementing sequentially from 1.

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  Code360. Do upvote our blog to help other ninjas grow. 

Live masterclass