Floyds Triangle In C

Example

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

Algorithm

  • Create variables that hold rows and column values as i and j. Take a number to display the rows as num and set the variable k to 1as its initial value.
  • Use nested for loops: Outer for loop starts its iteration i = 1 up to n rows. Inner for loop starts its iteration from j = 1 up to (j <=i).
  • Print the values of k.
  • Increment k by 1 or k = k + 1.
  • Jump to newline after each iteration of the inner for loop.
  • Stop
  • Outer for loop starts its iteration i = 1 up to n rows.
  • Inner for loop starts its iteration from j = 1 up to (j <=i).

Now we will create multiple Floyd's triangle programs using for loops in the C programming language.

Program to print the Floyd's triangle using for loop

Let's explore an illustration demonstrating how to display Floyd's triangle utilizing a for loop in the C programming language.

floyd.c

Example

#include <stdio.h>
#include <conio.h>
void main()
{
	int num, i, j, k = 1; 
 
	printf( " Enter a number to define the rows in Floyd's triangle: \n");
	scanf( "%d", &num);
	// use nested for loop
	// outer for loop define the rows and check rows condition
	for (i = 1; i <= num; i++)
	{
		// inner loop check j should be less than equal to 1 and print the data.
		for (j = 1; j <= i; j++)
		{
			printf(" %2d", k++); // print the number
		}
		printf( "\n");
		}   
		getch(); 
}

Output

Output

Enter a number to define the rows in Floyd's triangle:
6
  1
  2  3
  4  5  6
  7  8  9 10
 11 12 13 14 15
 16 17 18 19 20 21

2 nd time execution of the program:

Example

Enter a number to define the rows in Floyd's triangle:
5
  1
  2  3
  4  5  6
  7  8  9 10
 11 12 13 14 15

In the preceding code snippet, we displayed Floyd's triangle by employing a nested for loop structure. The outer loop is responsible for iterating through each row, while the inner loop handles the columns within Floyd's triangle. The outer loop begins executing from (i = 1) up to the nth row of the triangle. On the other hand, the inner loop initiates its execution from (j = 1) and continues until it reaches the value of i. Within this loop, we utilized the variable k, initially set to 1, to output the sequentially incremented values.

Program to print the Floyd's triangle using while loop

Let's explore an illustration demonstrating how to display Floyd's triangle utilizing a while loop in the C programming language.

program.c

Example

#include <stdio.h>
#include <conio.h>
void main()
{
	int num, temp = 1, temp1 = 1, k; 
 
	printf( " Enter a number to define the rows in Floyd's triangle: \n");
	scanf( "%d", &num);

	while(temp1 <= num)
	{
		k = 1;
		
		// inner loop check k should be less than equal to temp1 
		while (k <= temp1)
		{
			printf(" %2d", temp); // print the number
			temp++;
			k++;
		}
		temp1++;
		printf( "\n");
		}   
		getch(); 
}

Output

Output

Enter a number to define the rows in Floyd's triangle:
6
  1
  2  3
  4  5  6
  7  8  9 10
 11 12 13 14 15
 16 17 18 19 20 21

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

Let's explore a C program that utilizes a recursive function to display Floyd's triangle.

Recursion.c

Example

#include <stdio.h>
// declaration of global variables
int row = 1;
int k = 1;
int main()
{
	int num; // declare the variable
	printf( " Enter a number to define the rows in Floyd's triangle: \n");
	scanf (" %d", &num);
	Floyd_triangle( num); // call the Floyd_triangle function
	return 0;		
}

void Floyd_triangle (int num) // function definition
{
	int i;
	if (num <= 0) // if num should be less than 0, it returns return.
		return;
	
	// use for loop to define the rows
	for ( i = 1; i <= row; i++)
	
		printf( " %d", k++);
		
		printf ("\n");
		row++; // increment row by 1.
		Floyd_triangle (num - 1);
}

Output

Output

Enter a number to define the rows in Floyd's triangle:
6
 1
 2 3
 4 5 6
 7 8 9 10
 11 12 13 14 15
 16 17 18 19 20 21

Program to reverse the Floyd's triangle using for loop

Let's explore a C program that utilizes a for loop to display a reversed Floyd's triangle pattern.

Reverse.c

Example

#include <stdio.h>
int main()
{
	int num, i, j; // declare the variables
	printf ("Enter a number to define the rows in Floyd's triangle: \n");
	scanf (" %d", & num); 
	
	// initialize k and define the formula to get the reverse Floyd's triangle number
	int k = num * ( num + 1) / 2;
	
	// outer loop 
	for (i = num; i >= 0; i--)
	{
		// inner loop check the i condition 
		for (j = 1; j <= i; j++)
		{
			// print the decremented value of k by 1.
			printf (" %d", k--); /* at each iteration the value of k is decrement by 1. */
		}
		printf ("\n");
	}
	return 0;
	}

Output

Output

Enter a number to define the rows in Floyd's triangle:
6
 21 20 19 18 17 16
 15 14 13 12 11
 10 9 8 7
 6 5 4
 3 2
 1

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

Let's explore an illustration demonstrating how to display Floyd's triangle using a for loop in the C programming language.

program2.c

Example

#include <stdio.h>
#include <conio.h>
void main()
{
	int num, i, j, k = 1; 
 
	printf( " Enter a number to define the rows in Floyd's triangle: \n");
	scanf( "%d", &num);
	// use nested for loop
	// outer for loop define the rows and check rows condition
	for (i = 1; i <= num; i++)
	{
		// inner loop check j should be less than equal to 1 and print the data.
		for (j = 1; j <= i; j++)
		{
			printf(" *"); // print the star
			
		}
		printf( "\n");
		}   
		getch(); 
}

Output

Output

Enter a number to define the rows in Floyd's triangle:
6
 *
 * *
 * * *
 * * * *
 * * * * *
 * * * * * *

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

Let's explore an illustration on displaying Floyd's triangle utilizing a for loop in the C programming language.

program5.c

Example

#include <stdio.h>
#include <conio.h>
void main()
{
	int num, i, j, k = 'A'; 
 
	printf( " Enter a number to define the rows in Floyd's triangle: \n");
	scanf( "%d", &num);
	// use nested for loop
	// outer for loop define the rows and check rows condition
	for (i = 1; i <= num; i++)
	{
		// inner loop check j should be less than equal to 1 and print the data.
		for (j = 1; j <= i; j++)
		{
			printf(" %c", k); // print the Alphabets
			k++;
		}
		printf( "\n");
		}   
		getch(); 
}

Output

Output

Enter a number to define the rows in Floyd's triangle:
6
 A
 B C
 D E F
 G H I J
 K L M N O
 P Q R S T U

Input Required

This code uses input(). Please provide values below: