Generate Floyd’s triangle using MATLAB

What is Floyd’s triangle?

The Floyd’s triangle is a right-angled triangular array of natural numbers. It is defined by filling the rows of the triangle with consecutive numbers.It starts with the number one in the top left corner then two and three in the next row.

Some of the examples of Floyd’s triangle are :

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

MATLAB code

```

a=input('Enter no. of rows :&nbsp; ');
x=1;
for i=1:a
for j=1:i
fprintf('%d ', x);
x=x+1;
end
fprintf('\n');
end

```

Algorithm

• The user is asked to enter the number rows.This number is then stored in a variable.This number will decide how many rows have to be printed.
• Another variable is then initialized as one.This is because Floyd’s triangle starts with a one and then continues from there on.
• The first-row contains one. The second containing two and then three.Third-row contains four then five and then six and then so on.
• We define two “for loops” for our code. Here inside the first for loop, we provide the number of rows.
• Inside the second for loop, we print our numbers row by row.

Output

Below you’ll find the output of the program. Here we have asked to print for 2 then 5 and then 15 rows in the triangle.

SAURAB S

I am currently pursuing my btech in Electronics and communication engineering from Amrita Vishwa Vidyapeetham. My areas of interest are signal processing, Communication systems, Control system.

Recommended Posts

Test Post

17 Feb 2018 - Tutorial

Counters using conditionally executed subsystems | Simulink Tutorial

16 Feb 2018 - Simulink, Tutorial