HI this is Anil from LearningLad and welcome to another video on Java programming language.
In this video we will see how we can write a simple java program to display Floyd triangle.
now what exactly is this Floyd triangle?
Floyd triangle is a right angle triangle of natural numbers.
it is created by filling the rows of a triangle with consecutive numbers starting from 1
now if you look at this image then you can easily understand what exactly it is.
so here it is a right angle triangle you guys can see it is in right angle triangle shape
and it is filled with the consecutive natural numbers starting with one.
Now here in this triangle this is the First row, this is the second row, this is the third
row and this is the 4th row.
how many columns we have for every row?
now in the first row we have only one column.
in the First row we have one column and in the second row we can see that we have two
columns, in the third row we can see that we have three columns and in the 4th row we
have four columns
so now here we can say that the number of columns present in a row is equal to row number
and similarly if I have to write the fifth row here then the number here will be 11 cause
it is the consecutive natural numbers then 12 13 14 and 15.
since it is the fifth row we will have five columns in this fifth row.
now to print is triangle we need to understand that the number of columns present in a row
is equal to row number
so in the 4th row we have four columns Ok
now let's see how we can print this triangle.
here I have written the Skeleton of the Java program.
I have created the package LearningLad then I have imported disk scanner class
now we will be using this scanner a class for taking the input from the user and then
I have created this public class FloydTriangle and then we have this static void main method
from which our program start executing.
Now here how we are going to write this program is first we will ask the user for how many
rows he wants to print the triangle and then depending on the user input we will print
a triangle
so we need to store the user input and for that purpose I will create a variable of integer
type and i’m gonna call it as numrows for number of rows
All Right
now the next thing that we do is we will display a message asking the user to enter the number
of rows
so we will do that by using system.out.iprintln method and here I'll say enter the number
of rows
All right
now we have displayed the message to the user asking to enter the number of rows
now the user will enter a value
now we need to read that value and we need to store that in this numrows variable
so here what we do is we will use the scanner class and for that purpose I will create an
object of this scanner class
so it will be scanner and I will call it as scan which is the name of the object, equal
to new scanner and here we will pass in system.in
All right
now we have the object of this scanner class and we can use this object and we can read
the input
so you will use the nextInt method which is available in this scanner class.
it will read an integer value which is entered by the user and it will return that and we
will store that return value in this numrows variable
All right
now we have successfully read the value entered by the user and we don't have anything else
to read from the user
so I will close this scanner by using the close method
All right
now we know for how many rows we have to display this triangle
so we will start printing the triangle
now if you look at this pattern here then the values printed here are the consecutive
natural number starting from 1
so I will create a variable here to store the number that we have to print and I will
call it as num and I will initialise this variable with a value of one because if we
look at this image then the starting number is 1
All right
now the next thing that we do is we will print each row one by one and for that purpose we
will use of for loop and in this for loop we will create a variable and I will call
it as the row and I will initialise this variable with the value of 1 and then how many rows
that we want to print that will be the condition for this for loop
so it will be row less than or equal to now for how many rows we have to print this pattern
that value stored in this numrows variable which is entered by the user
so it will be numrows and then after printing each row we will increment the value of this
row variable so that it can work with the next row
so it will be row++
now here inside this for loop we will have the statements for printing the columns for
this row and after printing the column the last thing that we do for a row is we will
insert a new line so the next row will be printed in the next line
so for that purpose, we will use this system.out.println method
Here this statement will add only a newline nothing else
Ok
now for every row we need to display the columns
now the number of columns for a row depends on the row number and for printing the columns
we will use another for loop and here also we need a variable for the loop count
so I will call it as column and column variables value will start from 1
so I will initialize that with the value of 1 and then we need to have the condition.
so how many columns we want to print for a particular row?
That depends on the row number
here
so that value is available to us in this row variable
so the condition here will be column less than or equal to row and then after printing
one column we will increment the value of this column variable
now here for the First row this row variable will contain a value of one
so this for loop this inner for loop will print only one column that’s because when
this column variable contains a value of one, this condition will satisfy.
because one is less than or equal to 1 and it will print the number that we want to print
which we will write inside this for loop in the moment and then this column variable value
is incremented by one and at that time this condition will fail
so for the First row it will display only one column and then for this second row the
row variable will contain a value of 2 and this time this for loop will run 2 times that
is when this column variable contains a value of 1 and when this column variable contains
a value of two.
when this column variable contains a value of 3 and this condition will fail and then
it will stop printing that column for that particular row
then for the third row this row variable will contain a value of 3 and this for loop will
print three columns
ok
similarly this loop will run according to our input
Now here what we want to print in that column that we have the specified inside this inner
for loop
so we will print the value for the column using this system.out and I will use the format
Method here
if you want you can use the println method and you can specify the value that we want
to print but I'm using this format method just because I can format the value that I
want to print
now here we want to print an integer number
so it will be %d and what I want to do is I want to left justify the number which is
printed with 5 characters spaces
so I will use - 5 here.
So %- 5d and then what we want to print
so we want to print the number starting from 1
so that value is stored in this num variable.
So i’m gonna refer that num variable here
now we know that in this pattern the number which is printed here is in incremental order
so after printing the value stored in this num variable we will increment the value of
that num variable so that the next time when it is printed it will print the next number
ok
now we have written the code
so what I'm going to do is i’m going to build and run this
Enter the number of rows
I will say 5
1 2 3 4 5 rows and you guys can see it has painted the floyd triangle
I will run this again and this time i’m gonna enter 20 and now you guys can see it
as printed the floyd triangle for this number 20
Now here I have I used -5d here
this is to left justify the number which is printed with five character spaces
now you can experiment with this number and you can use different numbers also here
ok
so this is it guys this is how you guys can write a simple java
program to print the Floyd triangle
if you like this video hit the like button if you don't like it then hit the dislike
button
if you want to say something then write that in the comment box and if you want to watch
more tutorials like this then do subscribe to the channel
thank you for watching
I'll see you later the next video