C Program to Print Floyd’s Triangle

Floyd’s Triangle-It is a right angled triangle made by using natural numbers. It starts from 1 and goes on increasing consecutively in sequence.

In this C program we have to print Floyd’s triangle by taking the length of rows as input from the user.

C Program Output Example:
Input: Rows – 5
Output:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Before writing this C program, lets go through the concept we are gonna use in our program to create Floyd’s Triangle.

1. Concept

For creating Floyd’s Triangle we have to go through some steps given below:

  • first we have to take the input of number of rows upto which we have to create Floyd’s Triangle.
  • we will take an integer n=1 for consecutive number sequence.
  • There will be two for loops used in the program first for changing rows and second for printing consecutive numbers.
  • at the end of second loop we will print n and increase its value by 1.
  • In the last step we will use a print statement to change the row.

The concept of C programming you need to learn before writing the program is:-

1.For Loop

Kindly learn this topic and have a little knowledge about triangle formation with loops.

2. Algorithm

Here’s the algorithm of writing C program to print Floyd’s Triangle for the inputted number of rows by the user.

  1. START
  2. Initialize loop variables i=1 and j=1
  3. Take a number n=1 for printing consecutive numbers
  4. Take the input of number of rows from user.
  5. In the first loop we will give the condition upto number of rows.
  6. Second loop is used for printing the values in row.
  7. at the end of second loop print n and increase its value by 1.
  8. At the end of first loop print null value to change row(move to next line): printf(“\n”)
  9. STOP

3. C program for Floyd’s Triangle

After going through the algorithm now we will take a look at the code below. Take input from the user and move on to the C program.

#include <stdio.h>

#include <conio.h>
int main()
{
int i=1,j=1;
int n=1,rows;
printf("enter number of rows :");
scanf("%d",&rows);
for(i=1;i<=rows;i++
{
for(j=1;j<=i;j++)
{
printf("%d ", n );
n++;
}
printf("\n");
}
return 0;
}

4. Output

After writing the C program for Floyd’s Triangle, now check the output. As we have taken rows=5 from the user, you can take any number of rows. Let’s check the output.

C Program to Print Floyd’s Triangle
groot
groot

Leave a Reply

Your email address will not be published. Required fields are marked *