Home » Floyd’s Triangle in C

Floyd’s Triangle in C

by Online Tutorials Library

Floyd’s Triangle in C

The Floyd’s triangle is a right-angled triangle that contains consecutive natural numbers. In Floyd’s triangle, the number starts with 1 in the top left corner, and then it consecutive filling the defined rows through the numbers.

For example: suppose we have defined 5 rows in Floyd’s triangle, it generates the following pattern in increasing order:

Algorithm

  1. 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.
  2. 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).
  3. Print the values of k.
  4. Increment k by 1 or k = k + 1.
  5. Jump to newline after each iteration of the inner for loop.
  6. Stop

Now we write several programs of Floyd’s triangle using for loops in C language.

Program to print the Floyd’s triangle using for loop

Let’s consider an example to print the Floyd’s triangle using for loop in C.

floyd.c

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  

2nd time execution of the program:

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 above program, we have printed Floyd’s triangle using nested for loop. The outer loop is used to define the rows, and the inner for loop representing the columns of Floyd’s triangle. Where outer loop starts its execution from (i = 1) to the nth rows of the triangle. The inner loop starts its execution from (j = 1) and will be less than equal to i. Inside this loop, we used the k variable assigned by 1 and prints the incremented value by 1.

Program to print the Floyd’s triangle using while loop

Let’s consider an example to print the Floyd’s triangle using while loop in C.

program.c

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 consider a program to print the Floyd’s triangle in C using the Recursion function.

Recursion.c

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 consider a program to print the reverse Floyd’s triangle in C using for loop.

Reverse.c

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 consider an example to print the Floyd’s triangle using for loop in C.

program2.c

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 consider an example to print the Floyd’s triangle using for loop in C.

program5.c

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  

Next TopicC Header Files

You may also like