Hi this is Anil from LearningLad and welcome to another video on C programming language.
now in this video we will see how we can write a C program to print this pyramid pattern,
Now here you guys can see I have used the character ‘*’ star in this pattern.
You know you can use any character that you wish and you can print this pyramid pattern
now let's take a look at this pattern
this pattern is in pyramid shape and here I have divided this pattern in terms of rows
and columns
now these are the rows from up to down
so this is the First row, this is a second row, third Row, 4th row and 5th row and similarly
we have the columns here
So first column, second column, 3rd 4th 5th 6th 7th 8th and 9 columns are present in this
pattern
now if you look at this image then the number of rows present in this pattern is 5
So the total number of rows is 5
Now here when we try to print this pattern we will print this pattern row by row
so first we will print the First row and then the second row then the 3rd row then the 4th
row and then the 5th row
if you look at the First row then we have 4 spaces and if you look at the second row
then we have three spaces and then if you look at the third row then we have two spaces
in the 4th row we have one space and in the fifth row we have zero space
so the first thing that we do here is let's take a look at how we can calculate the spaces
that we have to print for a particular row
so here by looking at this we can say that the number of spaces that we have to print
for a row is equal to total number of rows - row number
so now if you look at this here then the total number of rows present in this pattern in
this image is 5 and for the first row, the row number is 1
so we need to print 4 spaces and you guys can see we need to print four space here.
similarly if you apply this logic for this third row then the total number of rows present
in this pattern is 5 and the row number is 3
So the space that we have to print is 2
So now we know how we can calculate the spaces that we have to print for a row.
so we can do that by using this formula that is
total number of rows - row number
will give us the number of spaces that we have to print for a row
Ok
now we have seen how we can calculate the spaces for the rows
now let's talk about how we can calculate the number of stars that we have to print
for each Row
Now if you look at this pattern pattern here then in the First row we have one star and
in the second row we have 3 stars and in the third row we have five stars and in the 4th
row we have Seven Stars and in the fifth row we have nine stars
now here for the example purpose i am using the character ‘*’ here in this pattern.
you can use any character for printing this pattern
so now If you look at the number of stars present in each row then we can form a formula
here and that is the number of stars that we have to print for a particular row is equal
to ( 2 * the row number) - 1
Here let’s apply this formula to this pattern
now let's take a look at the First row.
In the First row the row number is 1
So if you apply this formula then it will be 2 X row number
which is 1 - 1
it will be 1
so in the First row we have to print 1 star
now let's take a look at the 4th row
so now if you apply the same formula here
(2 X 4) - 1
so it will be 7
8 - 1
7
So in the 4th row we have to print Seven Stars
so now you know how to calculate the number of spaces that we have to print for a particular
row and also we know how to calculate the number of stars that we have to print for
a particular row
so now let's see how we can write a simple C program by using these two formulas and
let’s print this pattern
ok
now let's see how we can write the C program to print this pattern
so now in this video what we going to do is we are going to ask the user about for how
many rows he wants us to print the pattern and whatever the value use is going to enter,
according to that value we will print the pattern
so the first thing that we do here is we will declare some of the variable that we need
so the first variable that we need is for storing the user input for how many rows he
wants to print the pattern
so I will create a variable and I will call it as totalRowNo and in this variable we will
store the user input and then we will be using a for loop for printing the rows and for that
particular for loop we will use the loop counter variable and I am going to declare that here
I will call it as row and then for every row we need to print some spaces
so for that purpose I will declare a variable called space and then in every row we have
to print the star or any other character that you want to print
so I will call it as symbol here
in this video I will be printing star you know you can print any other symbol
so now the next thing that we do if we will ask the user about for how many rows he wants
to print the pattern
so we will use a printf function and we will display the message
the message will be Enter the number of rows and then we will read the input using the
scanf function and we will store that in this totalRowNo variable.
Ok
so now we know for how many rows we want to print the pattern
so the next thing that we do is we print this pattern row by row
so we use a for loop because we are going to be printing multiple rows we will use the
row variable and we will initialize that with the value of 1 and the condition will be the
number of rows that we want to print, is stored in this totalRowNo variable which is the value
entered by the user
so the condition will be row less than or equal to totalRowNo and after that we will
increment the value of this row variable
Ok
now here for every row what we going to do if we will print the space and then after
printing the number of spaces we will print the symbol and after printing one row we will
add a new line so that the next row printed will be in the new line and that's why we
will use this printf function and we will add a new line here
ok
now here what we have to do is we have to print the spaces and then we have to print
this symbols
so now let's take a look at printing the spaces
so if you look at this pattern then for every row we have to print more than one space and
that has to be calculated by using this formula
so we will use a for loop for and here it will be for spaces
so I will initialize this space variable with a value of 1
the condition will be how many space that we want to print
we can get that by using this formula and that is total number of rows - row number
So here the condition will be space less than or equal to total number of rows - row number
is available for us in this row variable
Ok
I’m gonna use the use the parenthesis here
ok and after that we will increment the value of the space variable
Ok
now this condition here this will calculate the number of spaces that we have to print
for a particular row Ok
now here we will use the printf functions and we will print a space
Ok
now we have written the code for printing the spaces
the next thing that we have to do is we have to write the code for printing the symbol
so now here again if you look at this pattern then the number of symbols printed for every
row is more than one and it has to be calculated by using this formula
so we will use a for loop here
for and it will be symbol equal to one
symbol less than or equal to
now we have to write the formula to calculate the number of symbols printed for every row
and that is 2 multiplied by row number and -1
so here the formula will be 2 multiplied by row number is available for us in this row
variable and it will be -1 here ok
I have used the parentheses to group this here
ok and after that we will increment the value of the symbol variable
so now here this condition will calculate the number of symbols that we have to print
for a particular row and we will print that here
so it will be printf statement and here the symbol that I want to print is star
Ok
so now before running this program let's take a look at this and I have made a silly mistake
and that is I have not used the & symbol for this totalRowNo variable when it is used in
this scanf function
so we have to do that
ok now let's build and on this program
enter the number of rows
5
and now you guys can see this pattern is printed for five rows
now let’s close that
lets build and run it again
this time I'm going to enter 20 rows
now you guys can see this pattern is printed for 20 rows
Now here if you want you can change the symbol that is being printed
for example if you want you can display the number 1 here
So let me quickly build and run this
20
You know it will look something like this and also what you can do is if you want you
can replace that spaces with some other character for example if you want you can and # here
so let me build and run this
20
You know it will look something like this
you know you can experiment with this code
so this is it guys
this is how you guys can write a simple C program to print this pyramid shape pattern
in C programming language
thank for watching
if you like this video then 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
for watching more tutorials like this, do subscribe to our channel
thank you for watching
I'll see you later in the next video