Hello and welcome back to this practical module….. and in this video, we are going to make a different program.
How will it be different from our other C programs, we will quickly understand it.
In this particular video we will be printing some different patterns with the help of C programming logic.
So, here we will be using few control flow statement, you all know what the control flow statements are.
So, basically in this video we will be printing different patterns.
What are those patterns? we will see those quickly over here.
First of all, we will talk about printing of the patterns.
So, the first pattern that we have, this half pyramid.
We all know that the pyramids that we have seen in Egypt, would be in real life or in photos.
So, we know that how is the pyramid, it is in triangular shape, it looks like an equivalent triangle in which there is a slope from both the sides and after going to the top there is a point.
So, this type of pyramid design printing patterns we are going to make through our program.
But the first pattern that we will make here, it is a half pyramid pattern like you can see here, our half pyramid pattern is of this way, if we have cut the pyramid from centre and we took only the right-hand side part, in which one edge is completely flat and straight at 90 degrees but our second edge is slanting.
So, this is a type of a triangle.
But what we are doing here is we are making use of an asterisk symbol, which means we are making the use of the star symbol.
And with the use of this star symbol, we will make this pattern, you can see that in our first row we have only one star.
If we talk about half pyramid here, in our first row we have one star, in second row we have two stars, in third row we have three stars, in the fourth row we have four and in the fifth row we have got five stars.
So, as many rows are increasing with us, those many stars will also increase.
If we are talking about this half pyramid.
So here we have to use such a logic, that suppose we have the first row, we will print only once star in a row.
After that we will again change the row, after changing the row, we have got the second row and in the second row what we will do is we will print two stars and again we will change the row, in the same way for three row, three stars, for fourth row four stars and for fifth row five star we will be using here.
In this way how many ever rows we want, lets suppose we want 10 rows.
Then there in the 10th row our 10 starts would be printed, in the half
Now what is the logic that we will have to use to print these stars in this particular pattern.
We will have to use here for loops.
And we all know about for loops.
What are for loops basically? The loops in which we have kept any condition, till the time the condition is getting satisfied.
The statement that we have written in loops, they get executed.
And if we deep dive in the program, in the logic, here we have to use nested loops, we had spoken about nested loops that in one for loop, we can write another for loop.
Which means, what will happen with this for the first for loop, the inside for loop will run that many multiple times.
And till the time it will run after that the outside for loop will starts its second iteration.
So, in this way our nested loops work, if you want to revise that concept again, you can definitely revisit our loop’s module and there you can see how our nested loops work, So, without any delay, we will start our C program. And and here we are making half pyramid.
So, we are going to see its logic over here.
Now we have come to our C program text editor, which is our Visual Studio Code and here we will start our program by the preprocessing command in which we will include stdio.h library.
Now, we will come ahead and start our main function, inside the main function you will see first of all, before all the things we have made three different variables, all three we have made of integer data type.
So, you can see that we have made “I” named variable, we have made “J” named variable and we have also made ”rows” named variable.
We will quickly understand why we have made it.
Like you must have seen the pattern over here for half pyramid.
So, how we will work.
For the first row we will make use of I variable and we will keep on increasing I’s value.
So, if I’s value is one, it means that with I we are representing the first row.
Now if I’s value becomes 2 since it is inside the loop obviously.
So, now when its value has become 2, then it is representing second row.
If it’s value becomes 3, then it will represent third row.
If its value becomes 10 then it will represent 10th row.
So, we are using I variable to represent our row.
Now we will go again into our program.
Here the I variable will be used for our row number.
Now if we talk about J.
So, J is also used to print number of starts or to print number of asterisk symbols.
Our I is the row number, if that is 1, we have to print 1 star.
If it is 2 we have to print 2 starts.
If it is 10, then we will have to print 10 starts.
In this way, I represent row number and what does j represents? J represents number of stars.
We will go and see how this logic will work.
Now we have made here IJ and rows named 3 variable.
What are rows basically? They are variable in which the user will put one value.
Which is the value that user is putting? User will put the value that I want 5 row’s half pyramid or I want 10 rows half pyramid or I want 15 row’s pyramid, I want 20 rows half pyramid or how many rows should be there in my half pyramid, that will he decided by the user over here.
And for that we are using one row named variable and in this variable we will be putting the user’s value.
It can be 5, 10, 2, 3,15, 25, 30, it can be any value.
That we will take in this row’s variable.
Now what has happened with us? Now we have got number of rows, how many rows pyramid should be there and along with that why should be make I and J variables.
Now we will go ahead and see how we are taking row’s input.
Now we are taking row’s input with the help of printf and scanf function.
In Printf we asked the user to enter the number of rows.
How many row’s pyramid do you require? Now the user has put 5.
For 5 what did the scanf do is it stores the 5 value that was put by the user in this row’s variable.
Now we have got number of rows according to which we have to design our half pyramid.
Now, you will be understanding that I am saying half pyramid over here because we know that pyramid’s structure is somewhat of this type, triangle.
If we split it from between, the half part that remains of the pyramid that we call as half pyramid and that's what we are going to print over here.
So, for that we will require the nested for loops, like you can see here we have made use of two for loops.
What is the first for loop doing? It will iterate on the I’s value, which means as and when the I’s value will increase, our for loops number of repetition will also increase.
So, I’s value we are starting from 1.
I’s value will start from 1 and slowly it will be 2, 3, till the time the condition is getting satisfied.
So, what is the condition over here? Condition is that till the time I’s value…the rows variable which was there, in which the rows have been passed by the user, till the time it remains less than that or equal to the I’s value, till that time our for loop will go on.
Which for loop? The outside for loop which is performing iteration for number of rows, it is performing the repetition.
So, if the I’s value is one over here.
So, we have seen that I’s value 1 means, 1st row, I’s value 2 means 2nd row.
In the same way I’s value 10 means the 10th row.
So now we will go ahead and we will come into our for loop, which is defined from here till here.
So now you will see that as soon as we come into the for loop we got one more for loop and this for loop basically runs on what? It runs in J variable.
As and when the J’s value will increase, those many times our for loop will keep on going.
So, J’s value we have started with 1.
So, after starting from 1 what is the condition that we have put? We have put the condition that either J’s value should be less than I or it should be equal to I.
What does this mean? Suppose my I’s value is 2, that means we have now come to the second row of the half pyramid.
I’s value 2 means the 2nd row.
So, in the second row we know that in the second row we have to print 2 starts.
So, now to print two starts, the 1st time loop will run, so the j’s value will start
For J’s value to be 1, the loop ran, we have put one start there.
Now again the J’s value has increased and it has become 2.
Since both I’s and J’s value was 2.
Still this condition is getting satisfied.
So, even for the second time, for loop will print the start and it will increase the value of J.
Now, J’s value will increase to 3 but as soon as we will pass the J’s value as 3.
Then you will see that 3 is not less than or equal to I because I’s value is 2.
So, that’s why this inside loop will not run for the value.
And it will come to the outside loop.
As soon as the I’s value will increase again.
Earlier I’s value was 2 now it will become 3.
We will again run the inside for loop for 3 and this time it will run for 3 times because if it is a third row, we have to print 3 stars.
So, you can see that the outside for loop is going on as per rows and the inside for loop basically tells us for each row how many stars has to be printed.
That’s why inside the inner for loop, we have written this printf statement in which we are printing this start.
So, you must have seen that the pattern that we want to print, in that there are a lot of stars that are printed.
But we have written the star in the program only once.
So, this is the wonder of control flow statements and for loops.
For loop helps us run one statement continuously but continuously in which way it will run? In the 1st line it will run once, in the second row it will
On the 10th line it
will run 10 times.
So, this is about our for loops, nested for loops.
So, here you will see that the 1st for loop is runs for the rows.
Second for loop runs to print the asterisk.
And where the second for loop is ending, after that we have written one more printf statement and for that we have made use of “/n”.
Now we all know that /n means to go on a new line.
Like our asterisk has got printed, 1 for loop.
Which means for 1 row.
Now we will run the printf statement and come to the next line and again the loop will start running.
So, this is the type of the concept.
Here you might find the topic to be complex.
But as soon as you will see its output, you will definitely understand it and after that then you will try to print your own patterns.
We will quickly see what is the output of this logic in so for that, on our terminal.
I will simply run it.
As soon as I run it, you can see that I have been asked here that enter the number of rows.
Now, if we have to understand that how it works, we will consider here less number of rows here, which is three.
As soon as you enter three, over here you can see that with us, we have got a pattern of half a pyramid made of three rows.
So, you can see how beautiful this pattern looks.
As and when we will increase the number of rows, it is 5 or 10 or 15, those many rows will increase here and the half pyramid will be that big.
Now here we will understand it with a logic, this star pattern, this half pyramid pattern, how was it printed.
Suppose, like in row we passed here 3, similarly the user passed 3.
So, this means that our pyramid will be of 3 rows, here 3 rows will be made one by one.
So, for the 1st row our for look goes one and its value is “i=I”.
As soon as we check the condition that “i<=rows” and we know that in rows 3 is stored.
So, i<=3, this condition is true, that’s why we have come into for loop.
In for loop as well, the loop has started with “j=1” and j’s value is 1.
So, here j<=i is getting checked.
This means that 1<=, I value is also 1 over here.
So, is i=j? So, ever here this condition is satisfied.
And here the printf statement has run, in which we have printed this star symbol, which has come over here with us.
Now this loop again ran and started running for the other value.
Since we had done j++, now the j’s value is 2.
So, again this condition will be checked that 2<=1.
But that condition is not satisfied as neither 2 is less than 1 or equal to 1.
2 is greater than 1.
So that’s why this condition will not be satisfied here, so this loop will break from here, it will come out and this condition will run here which is \n.
This means that instead of the 1st line, you have to come to the 2nd line.
Now with the \n we have come to the second line.
So, this means that our outside for loop is also over and now the I’s value because of i++ has become 2.
Again it will be checked if 2<=rows, in the rows it is 3.
So, 2 is less than rows, which means 2<3.
So, again we have come inside the loop.
Now again the inside loop will be run.
Now the j’s value is again 1.
So, j<=2 because here I’s value is 2.
So, here the j’s condition will be checked with whom? With the current value of I which is 2.
So, here j=1, so 1<=2 and we will come inside of it and inside this star will be printed.
After the star gets printed we will again go out of the for loop and we will increase the j’s value and we will make it 2.
After making it 2 again the condition will be checked and this will be satisfied.
Because 2<=2 itself and again we will execute the printf statement.
And even second star will print and come to us.
We will again go and make the j’s value as 3.
But this condition will not be satisfied as i’s value was maximum 2.
So, again the 3rd time the loop will not run.
The 3rd star will not be printed.
Because of which we will come out of the for loop.
And the printf statement will be run to switch on the next line.
Now we will come on to the next line here.
So, in this way I’s value will be 3.
Our inside loop will run three times, it will print start three times and three times star will be printed.
When it will go to do it for the fourth time.
The condition will not be satisfied.
We'll come outside of the internal loop.
Then we will check in the outside loop that even after increasing I’s value more than 3, is it satisfying the condition.
If it doesn’t do that means even the outside loop has got over.
And our program has also got over as we wanted half pyramid of three rows.
And that we have got here.
So, this was about making 3 rows half pyramid.
In the same way if I want that I want to make 10 rows half pyramid.
So, we will see here, how will it run.
So, as soon as I run it, you can see that now I have to make 10 rows pyramid.
I will write 10 here and you can see that I can see 10 rows pyramid made here.
I will extent the terminal a little.
So, that I can see the right output.
And you can see that we have got complete 10 rows over here.
So, in this way nested for loops are working.
If you have to print half pyramid then it gets printed in this way.
So, this was about the half pyramid and now is the time to see the next pattern, which is a full pyramid.
Now we will quickly go into our presentation and we will see that here the second pattern is of full pyramid.
Till now we had made half pyramid.
And we saw only half the part printed.
Now we are going to print the entire pyramid shape structure.
And for this you can see that we have to leave some empty space on the both left and right side.
Only after that we would be getting this equivalent triangle look.
So, to print this pattern we would first have to understand its concept and for that we have created a visual representation here in which you will see that what do you have here is we have a pyramid made of the triangular shape design.
Which you can see inside the grid shape.
So, this is our first row what do you have to do in the first row? We have to leave the space this way and we have to print the 1st star over here.
After that we have to print three stars in this way.
And after that, we have to print five stars in this way.
And then slowly how many rows we want.
Like in this particular example there are five rows, so we made 5 rows entire pyramid and kept it ready.
So how does it get ready that we will see over here.
We'll see here that we have taken five rows that's why the I’s value is 1, 2, 3, 4, 5, we are getting here.
In the same way if we talk, if we have 5 rows, then how many stars are getting printed in the fifth row? So, inside the fifth row the stars that are getting printed are 1, 2, 3, 4, 5, 6, 7, 8, 9.
Like we have seen in the half pyramid, if ours is a 10th row.
Then there 10 stars used to get printed.
But here if I talk about the full Pyramid, in the fifth row we are printing all nine Asterix and nine stars.
In between these we have to bring about some relationship.
Like what was the relationship that we had brought about in half pyramid, whatever is the I’s value, those many times our symbols, star marks will get printed.
Now, what we will have to do here? First of all, we have to see spaces.
After how much space we have to print the star.
So, you can see that for first row we have printed one star but the space that we left from the left hand side that was 4.
Which means for the 1st row we left 4 space.
For the second row, we left 3, for the 3rd row we left 2, for the 4th row we only left one space.
After that we started printing and we did not leave any space for the 5th row.
So if we had 10 rows over here, so basically what would have happened here? Then here in the first row, four spaces will not have been left.
More than these would have left.
So that our pyramid starts making from the front and the last start should print in the corner.
So we have to make this kind of representation with the help of C Program.
For that we will have to make few logics and few conditions.
How will be those conditions, we will see that here.
You will see that for the 1st row we have left 4 spaces, which has been show
to us with the help of a table.
When the I’s value was 1, how much space did we leave.
We had left 4 spaces.
When the I’s value was 2, which means 2nd row came to us.
So, we left 3 spaces.
In the same way when I’s value was 3.
We left two spaces.
When I’s value was 4 space, we left only one space.
As soon as I’s value became 5, which means that we came on the 5th row.
We left 0 spaces and directly we printed stars.
So, to bring the relationship between them.
How will we know on how many rows, how many spaces to live and how many not to leave? For that we will make use of logic.
Which is rows-1.
Suppose our pyramid is going to be made of 10 rows.
So, what we have to do is for every row, 10-, whatever is the row number.
This is what we have to do, Suppose, if it is the 1st row.
In this case we will take 5 rows instead of 10, And we will see here.
What did we do? We took the 1st row, which means we took I’s value as 1.
And how many rows have to be printed? Five rows have to be printed.
So, by rows-1 logic.
5-1=4 spaces we have to leave.
In the same way if we talk about the second row.
So, here 5-I’s value which is 2, which means the second row.
5-2 which means 3 spaces we will have to leave.
Along with that for the 3rd row, 5-3 which means 2 spaces we will have to
For the 4th row,
5-4 which means 1 space we will have to leave.
For the 5th row
5-5, which means 0 spaces we will have to leave.
So, this was about how we can tell that how much space we have to leave for which row.
Now we will go ahead and we will see one more logic, one more condition, which we have to use here.
First for the half pyramid what we used to do for the 1st pyramid.
We used to print one star.
In the same way for 5th row, we used to print 5 stars.
But here you can see that first row, we have printed 1 star, which is okay.
For the 2nd row,
instead of 2 we have printed 3 stars.
To get a fully pyramid design.
Along with that when there was 3rd row we printed 5 stars over here.
For the 4th row we
have printed 7 stars.
And for the 5th row we have printed 9 stars.
Which you can see in this table, where we have written, when I’s value was 1, which means in the 1st row we printed one star.
In the 2nd row we
printed 3 stars.
In the 3rd row we
printed 5 stars and in the 4th row we printed 7 stars.
And in this way as and when the rows will increase.
Those many stars will also increase.
Now how did the starts number value came? How did this stars number value came? So, the stars number’s value came in this way, that what we did was we used this logic that 2*whatever is our row’s number, which means the I’s value.
So, 2* I’s value-1.
This kind of logic we have used.
Why? Because we wanted number of stars.
You will see that how this patter is going on, when I’s value was 2, we put 3 stars.
How did those 3 stars come? What we did was 2*I, I’s value was 2 here, so 2*2 becomes 4 and 4-1 is 3 over here.
So, in this row 3 stars will be printed.
In the same way when I’s value was three.
What we did was 2*3, which was 6 and we did 6-1 and we got 5 stars for 3rd row.
In the same for the 5th row, when I’s value was 5, we multiplied 2*I
which means 2*5, which is 10, as soon as we do minus 1 comes 9.
So, to print the stars, how many stars have to come in which row, for that we have put 2*i-1 logic over here.
So, we are going to go by this concept, to print this particular pattern, to print the full pyramid and for this we will need 3 for loops, obviously 3 for loops will be nested.
How will they be nested, we will see that.
First for loop we will require to change the rows.
If the 1st row is
over print the 2nd row, print the 3rd row, print the 4th row.
In this way the rows will keep increasing.
So, one loop is for that.
Second loop would be to print the number of spaces.
In the 1st one 4,
next one 3, in the next one 2 and in the next 1.
In this way for spaces we will require one more for loop.
And to print Asterix.
In the 1st row 1
Asterix, in the second row 3 Asterix.
In this way to print the number of stars we will require one more for loop.
So, here in total 3 for loops will be required compared to that we had used only 2 for loop for half a pyramid.
So, we will quickly see its program and understand how its program is running.
Now we have come into fully pyramid program.
Here you can see that now we have made 4 variables, I, j, k and rows variable.
I will basically represent number of rows.
What will j represent? J will represent number of spaces, how many spaces to give in which row and what will k represent.
K will represent how many starts to give for which row.
And we know that the rows named variable that we have made, it is made for how many rows pyramid there should be from the user.
So, we will quickly see the printf, scanf function, going ahead.
We will straight away see our for loop, how is for loop working.
Frist for loop you can see is very same, it is going on like our last for loop which was in half pyramid, in which we had started I’s value from 1 and till <= rows we will run it.
You can see that along with that we have passed one more parameter.
That is k=0.
Why have we passed this parameter? Because first time we have to keep k’s value as 0.
Why we have to keep k’s value 0, we will see that here.
For now you don’t concentrate on that.
For now we will remove this k all together.
So, that there is not much confusion that is created over here for us.
So, how we have got a simple, normal for loop, which comes to us for half pyramid, in the same way it has come here as well for counting the rows.
So, I isr representing rows.
Now we will come inside, the one for loop inside that starts with j, what will that print? Number of spaces.
So, what did we discuss? We will do rows-i.
Whatever is the value of I, we will minus it from the rows.
Suppose we have 5 rows pyramid, whatever is the value of 5-I at that time I could have subtracted it and till that time we would print that many spaces.
So, in the same way, you can see this logic, we have put the condition till row-I, till the time j-I’s value is not equal, till that time this for loop will go on.
Now the number of spaces have printed, after this for loop has got printed.
We have come ahead and ahead and we have put one more for loop, which we have discussed, for what we will be using it, to print our stars.
So, to print the stars we have put this third for loop.
Which is starting with k.
We had just discussed the logic of printing stars.
Which was 2*I-1, This kind of condition we have put in this for loop and we have printed the stars.
So, how is the work going on here? First the main for loop is targeting for the number of rows.
As soon as we come in, one for loop is printing the number of spaces.
With which logic? With rows-I logic that how many total rows are there of our pyramid, that minus the current row that is going on, then it will give us the number of spaces and those many times our spaces will get printed with the help of for loop for that particular row.
Now the spaces have printed, now we will come to the next for loop.
And in the next for loop we will print the number of stars.
To print the number of stars we saw, if the stars have to be printed in the second row.
So, how many stars will be printed in it? 2*1-1, which means for the second row 2*2 which means 4 and 4-1 means 3.
So, 3 stars would be printed for what? For the second row.
And that’s why this for loop will run 3 times based on this condition and as soon as this condition is not satisfied, we will come out of this for loop and our row will change with the help of "\n”.
So, this was about how the full pyramid works.
So, this was about how a full pyramid works, how we print it.
And to see how it works.
You can understand the code and execute in your computer and even here we will be showing you how this program runs and gives us the output.
So, for that we will run this program.
And as soon as we will run it.
You can see that here we are asked, how many number of rows we have to take.
So, we will tell the number of rows over here.
Suppose you take 3, if I give 3 as number of rows and as soon as I enter, you can see that here with me half pyramid has not printed.
full pyramid has got printed in which what happened is inside the 1st row, 2 spaces have been printed.
After the one star got printed.
Then only one space got printed and 3 stars got printed.
And in the 3rd row 0
space got printed.
And we have got 5 stars printed.
In the same way if I try for one more number and if I tell it this time that you try for 9 rows.
So, you can see that I have got even the 9 rows pyramid ready with me.
How? Because we have used here 3 for loops.
First for loop we used for what? For iterating rows.
To keep on increasing the rows.
Second for loop we have used to decide the number of spaces.
Which are these spaces, in every row how much space we have to live, which is decreasing slowly as and when the rows are increasing.
And the 3rd for
loop we have used to pint these stars.
These 1, 3, 5, 7, 9, these kinds of stars that are getting printed.
To print those we used our third for loop.
So, this was the entire concept how we can print this pattern.
Apart from these patterns there are many other patterns, in which with the help of for loops we can make different patterns, you can implement different logics, that can be inverted pyramids or making the pyramids using other numbers or any other patterns, you can try them all.
Once you learn making this half and full pyramid.
If you think that there is any part of this video or this topic, you might have not understood then you can ask us any query or question related to that topic without any inhibition.
You just have to go on forums.learnvern.com, you have to
type your query there in your own language and we will come up with the
solution or the answer as soon as possible in your own language.
Apart from this if you wish to have a discussion on any topic.
Even that you will be able to do on forums.learnvern.com.
Till that time thank you so much.
Share a personalized message with your friends.