Special Programs in C − Pyramid of Stars

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this presentation we will consider one special problem which is called pyramid of stars we would like to print a pyramid of stars on the screen let's see how it looks like if you want to print a pyramid of stars like this which consists of three rows and this out looks like right I simply asked user how many rows you want in your Pramod the person might say 3 then I would print the pyramid with 3 rows similarly if we want the parameter of 10 rows then this is how it looks like and even if we want greater number of rows in our pyramid then we can also do that like this in this brahmic there are 20 rows you can see isn't that beautiful if you would like to print this pyramid on your own then you might have to have a little understanding related to some topics basically in order to print this pyramid what you require is a basic understanding of matrix what a matrix is all about and you must also have a little common sense so let's get started now as you can see this is our 2 into 2 matrix right here you are having 2 rows and 2 columns that's why it is a 2 into 2 matrix suppose J represent columns and I represent rows I would like to fill this matrix with stars using programming before that I would like to ask you one question how do we able to identify the location of this box this box location is 1 1 that means first row and first column similarly this box location is first row second column this box location is second row first column and this box location is second row second column isn't that so after knowing the locations we can use the programming technique to actually print stars in these boxes how do be able to do that here is the code which will actually print stars in these boxes for I equals to 1 I less than or equals to 2 I plus plus and inside this for loop you are having another for loop and in this for loop you are having printf function which print stars in these boxes now think about it how this particular nested for loop structure works when I is equals to 1 as the condition is satisfied we come inside this for loop right and when we come inside this for loop we find another for loop as J is equals to 1 initially we check the condition as condition is satisfied with simply print star suppose I represent rows and J represent columns therefore when I is equals to 1 and when J equals to 1 we would be able to print star after that we come to this point that means incrementing the value of J has previously J is 1 after increment it becomes 2 we check the condition is 2 less than or equal to 2 as 2 is equals to 2 condition is satisfied become inside the spot open print star now this is for I equals to 1 as I still 1 and J equals to 2 that means first row and second column that means at this particular location we would be able to print star now we again increment the value of J after printing star now J becomes 3 as 3 is not less than or equals to 2 therefore we come outside of this for loop right why do we want to here because here only 2 columns right after completion of this for loop we come to this point that means incrementing the value of I previously I was one after increment it becomes 2 now do is equals to 2 condition is satisfied we again come inside this for loop again J is initialized to 1 and again the condition is satisfied and we simply print star but at which location I is 2 and J is 1 that means second row and first column that means star is printed over here after printing this star we increment the value of J J now becomes 2 and again the condition is satisfied we again print star but for this time we are printing it for I equals to 2 and J equals to 2 that means at this particular location now you can easily see how this nested for loop structure is mimicking the behavior of a matrix right this represent number of rows and this represents the number of columns and in the row wise manner we would be able to print stars on the screen similarly for a 4 into 4 matrix we would be able to print stars using again this nested for-loops structure but the only difference lies is that here you are having 4 into 4 matrix therefore the number of rows should be changed to 4 and number of columns should also be changed to 4 and the rest of the thing is exactly the same now I hope you understand how to be able to print stars on the matrix using nested for-loops structure now it's high time to get into the problem see this particular structure as you can clearly see this is a pyramid right we can easily represent this pyramid using matrix how can we do that here you can see I'm representing this pyramid using a 4 into 7 matrix there are 4 rows and 7 columns right similarly we would be able to represent this pyramid also with the help of a matrix which consists of three rows and five columns our first target is to know when you are having n number of rows then how many number of columns you actually require obviously for different number of rows you are having different number of columns like in this case you are having three rows and five columns in this case you are having four rows and seven columns therefore it is a requirement that if we want to represent a pyramid like this we first need to know how many number of columns we required corresponding to the number of rows we have if we are having three rows then we require five columns as you can clearly see here if we are having four rows then we require seven columns if we are having five rows then we require nine columns you can easily see this and if we have six rows then we require eleven columns you can easily see the pattern here if you're having n number of rows then we can clearly say that we require 2 n minus 1 columns so we can easily obtain the number of columns by using this formula right therefore our first task is finished that means for our nested for loop structure we require n number of rows and 2n minus 1 number of columns right now let's move on to our next task as after completion of this task that how many number of columns we require when we have n number of rows we need to know how to be able to print stars like this in this matrix remember that we do not have to fill all these boxes with stars we just have to fill few of them so that we would be able to obtain this pattern right let's try to understand how to be able to print this pattern as you can see in the first row only one star is printed and that too in the fourth column in the second row three stars are printed from second row third column to second row fifth column similarly five stars are printed from third row second column to third row sixth column and similarly seven stars are printed from fourth row first column to fourth row seventh column if you observe this carefully this four is nothing but n only is in that so as you're having n number of rows at a particular instance of time here you are having four rows therefore we can say that this four is nothing but n only right similarly we can say that this is n minus 1 and this is n plus 1 this is n minus 2 and this is n plus 2 similarly this is n minus 3 and this is n plus 3 now let's try to understand what is happening here when you are in the second row you are printing stars from n minus 1 to n plus 1 right can we write this n minus 1 as n minus 2 minus 1 as 2 minus 1 is 1 therefore we would be able to obtain one here why I am writing 2 to actually relate this to the second room this is n minus 2 minus 1 and this is n plus 2 minus 1 so we can say that we are printing stars from n minus 2 minus 1 to n plus 2 minus 1 similarly for the third row we can say that we are printing stars from n minus 3 minus 1 to n plus 3 minus 1 similarly we can say that for the fourth row we are printing stars from n minus 4 minus 1 to n plus 4 minus 1 right and also for the first row we are printing stars from n minus 1 minus 1 to n plus 1 minus 1 which is equals to n only so what we are actually obtaining from this particular observation when J is greater than or equals to n minus I minus 1 and J is less than or equals to n plus I minus 1 then only we should print star otherwise we simply print a blank space here I represents the row suppose you are in the second row then you should print stars from n minus 1 to n plus 1 right this is the idea we need to adopt as this is the correct code we can add this in our actual code inside this nested for loop structure now you can add this whole code snippet and it will definitely print the pyramid as needed let's try to execute the code and see whether we would be able to get the pyramid or not this is the same code that we had seen previously the only changes that are declared three variables and I and J after that we have a printf function that simply prints how many rows you want in your pyramid and after that we simply accept the input that is our number of rows and then the same code is used to print the pyramid according to the number of rows let's see what could be the output it asks us to how many rows you want in your pyramid let's say I entered 10 and hit enter as you can see a pyramid is printed on the screen with 10 Drew's I hope you got it thank you for watching [Applause] [Music] you [Music]
Info
Channel: Neso Academy
Views: 452,340
Rating: 4.9069939 out of 5
Keywords: pyramid program, pyramid program in c, pyramid of stars, pyramid of stars in c, pyramid of stars c program, pyramid of stars using nested for loops, star pyramid, star pyramid in c, c programming tutorial, tricky c programs, tricky c questions, c language, c programming lectures, c programming, c programming for gate, gate c programming, gate cse, gate computer science
Id: KdM6OrvcjPI
Channel Id: undefined
Length: 11min 6sec (666 seconds)
Published: Sun Aug 05 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.