
Special Programs in C − Pyramid of Stars
Special Programs in C − Pyramid of Stars
C Programming \u0026 Data Structures: Special C Programs − Pyramid of Stars.
Topics discussed:
1) How to print a pyramid of stars using only for loops and if-else condition.
C Programming Lectures: https://goo.gl/7Eh2SS
Follow Neso Academy on Instagram: @nesoacademy(https://bit.ly/2XP63OE)
Follow me on Instagram: @jaspreetedu(https://bit.ly/2YX26E5)
Contribute: http://www.nesoacademy.org/donate
Memberships: https://bit.ly/2U7YSPI
Books: http://www.nesoacademy.org/recommende…
Website ► http://www.nesoacademy.org/
Forum ► http://forum.nesoacademy.org/
Facebook ► https://goo.gl/Nt0PmB
Twitter ► https://twitter.com/nesoacademy
Music:
Axol x Alex Skrindo - You [NCS Release]
#CProgrammingByNeso #CProgramming #PyramidCProgram #PatternProgram
Content
0.06 -> in this presentation we will consider
1.8 -> one special problem which is called
3.56 -> pyramid of stars we would like to print
7.47 -> a pyramid of stars on the screen let's
10.83 -> see how it looks like if you want to
13.049 -> print a pyramid of stars like this which
15.96 -> consists of three rows and this out
17.94 -> looks like right I simply asked user how
21.24 -> many rows you want in your Pramod the
23.16 -> person might say 3 then I would print
25.8 -> the pyramid with 3 rows similarly if we
29.22 -> want the parameter of 10 rows then this
31.26 -> is how it looks like and even if we want
35.19 -> greater number of rows in our pyramid
36.899 -> then we can also do that like this in
40.969 -> this brahmic there are 20 rows you can
43.5 -> see isn't that beautiful if you would
47.28 -> like to print this pyramid on your own
49.5 -> then you might have to have a little
51.149 -> understanding related to some topics
53.96 -> basically in order to print this pyramid
56.579 -> what you require is a basic
58.62 -> understanding of matrix what a matrix is
60.899 -> all about and you must also have a
63.359 -> little common sense so let's get started
66.439 -> now as you can see this is our 2 into 2
70.32 -> matrix right here you are having 2 rows
73.14 -> and 2 columns that's why it is a 2 into
75.81 -> 2 matrix
76.56 -> suppose J represent columns and I
80.189 -> represent rows I would like to fill this
83.1 -> matrix with stars using programming
85.5 -> before that I would like to ask you one
87.78 -> question how do we able to identify the
90.329 -> location of this box this box location
93.81 -> is 1 1 that means first row and first
97.14 -> column
98.159 -> similarly this box location is first row
100.979 -> second column this box location is
103.829 -> second row first column and this box
106.35 -> location is second row second column
108.329 -> isn't that so after knowing the
111.06 -> locations we can use the programming
112.89 -> technique to actually print stars in
116.07 -> these boxes how do be able to do that
118.619 -> here is the code which will actually
120.78 -> print stars in these boxes for I equals
124.92 -> to 1 I less than or equals to 2 I plus
127.469 -> plus and inside this for loop you are
130.229 -> having another for loop and in this for
133.11 -> loop you are having
133.88 -> printf function which print stars in
136.07 -> these boxes now think about it how this
138.56 -> particular nested for loop structure
140.69 -> works when I is equals to 1 as the
143.84 -> condition is satisfied we come inside
146.15 -> this for loop right and when we come
148.34 -> inside this for loop we find another for
150.38 -> loop as J is equals to 1 initially we
153.2 -> check the condition as condition is
154.73 -> satisfied with simply print star suppose
157.46 -> I represent rows and J represent columns
161.26 -> therefore when I is equals to 1 and when
163.91 -> J equals to 1 we would be able to print
166.16 -> star after that we come to this point
168.71 -> that means incrementing the value of J
171.14 -> has previously J is 1 after increment it
174.02 -> becomes 2 we check the condition is 2
176.27 -> less than or equal to 2 as 2 is equals
178.73 -> to 2 condition is satisfied become
180.71 -> inside the spot open print star now this
183.8 -> is for I equals to 1 as I still 1 and J
188.45 -> equals to 2 that means first row and
191.63 -> second column that means at this
194.9 -> particular location we would be able to
196.91 -> print star now we again increment the
199.37 -> value of J after printing star now J
202.07 -> becomes 3 as 3 is not less than or
204.65 -> equals to 2 therefore we come outside of
207.35 -> this for loop right why do we want to
210.29 -> here because here only 2 columns right
213.76 -> after completion of this for loop we
216.08 -> come to this point that means
217.16 -> incrementing the value of I previously I
220.01 -> was one after increment it becomes 2 now
224.45 -> do is equals to 2 condition is satisfied
226.55 -> we again come inside this for loop again
229.37 -> J is initialized to 1 and again the
232.55 -> condition is satisfied and we simply
234.14 -> print star but at which location I is 2
237.68 -> and J is 1 that means second row and
241.1 -> first column that means star is printed
244.01 -> over here after printing this star we
246.32 -> increment the value of J J now becomes 2
248.42 -> and again the condition is satisfied we
250.19 -> again print star but for this time we
252.68 -> are printing it for I equals to 2 and J
254.9 -> equals to 2 that means at this
257.54 -> particular location now you can easily
259.609 -> see how this nested for loop structure
262.16 -> is mimicking the behavior of a matrix
264.8 -> right
266.639 -> this represent number of rows and this
269.19 -> represents the number of columns and in
272.129 -> the row wise manner we would be able to
274.259 -> print stars on the screen similarly for
277.8 -> a 4 into 4 matrix we would be able to
280.139 -> print stars using again this nested
282.81 -> for-loops structure but the only
284.49 -> difference lies is that here you are
286.169 -> having 4 into 4 matrix therefore the
288.599 -> number of rows should be changed to 4
290.31 -> and number of columns should also be
291.9 -> changed to 4 and the rest of the thing
294.419 -> is exactly the same now I hope you
296.669 -> understand how to be able to print stars
299.34 -> on the matrix using nested for-loops
301.229 -> structure now it's high time to get into
303.78 -> the problem see this particular
306.09 -> structure as you can clearly see this is
308.729 -> a pyramid right we can easily represent
311.52 -> this pyramid using matrix how can we do
314.34 -> that
321.01 -> here you can see I'm representing this
324.11 -> pyramid using a 4 into 7 matrix there
328.16 -> are 4 rows and 7 columns right similarly
332.75 -> we would be able to represent this
334.49 -> pyramid also with the help of a matrix
337.24 -> which consists of three rows and five
340.76 -> columns our first target is to know when
343.91 -> you are having n number of rows then how
345.98 -> many number of columns you actually
347.51 -> require obviously for different number
349.94 -> of rows you are having different number
351.23 -> of columns like in this case you are
353.69 -> having three rows and five columns in
356.69 -> this case you are having four rows and
358.49 -> seven columns therefore it is a
361.1 -> requirement that if we want to represent
363.05 -> a pyramid like this we first need to
365.72 -> know how many number of columns we
367.37 -> required corresponding to the number of
369.14 -> rows we have if we are having three rows
372.16 -> then we require five columns as you can
374.96 -> clearly see here if we are having four
377.12 -> rows then we require seven columns if we
379.76 -> are having five rows then we require
381.65 -> nine columns you can easily see this and
383.95 -> if we have six rows then we require
386.96 -> eleven columns you can easily see the
389.78 -> pattern here if you're having n number
391.64 -> of rows then we can clearly say that we
394.76 -> require 2 n minus 1 columns so we can
399.08 -> easily obtain the number of columns by
401 -> using this formula right therefore our
404.06 -> first task is finished that means for
408.11 -> our nested for loop structure we require
410.09 -> n number of rows and 2n minus 1 number
413.27 -> of columns right now let's move on to
416.93 -> our next task as after completion of
419.48 -> this task that how many number of
421.4 -> columns we require when we have n number
423.71 -> of rows we need to know how to be able
427.07 -> to print stars like this in this matrix
429.61 -> remember that we do not have to fill all
432.26 -> these boxes with stars we just have to
436.16 -> fill few of them so that we would be
437.72 -> able to obtain this pattern right let's
441.08 -> try to understand how to be able to
442.91 -> print this pattern as you can see in the
446.63 -> first row only one star is printed and
448.79 -> that too in the fourth column in the
451.49 -> second row three stars are printed from
454.78 -> second row third column to second row
457.12 -> fifth column similarly five stars are
460.66 -> printed from third row second column to
464.29 -> third row sixth column and similarly
467.08 -> seven stars are printed from fourth row
469.87 -> first column to fourth row seventh
472.06 -> column if you observe this carefully
474.22 -> this four is nothing but n only is in
477.04 -> that so as you're having n number of
479.02 -> rows at a particular instance of time
481.09 -> here you are having four rows therefore
483.85 -> we can say that this four is nothing but
486.07 -> n only right similarly we can say that
489.28 -> this is n minus 1 and this is n plus 1
492.55 -> this is n minus 2 and this is n plus 2
495.76 -> similarly this is n minus 3 and this is
498.1 -> n plus 3 now let's try to understand
501.25 -> what is happening here when you are in
503.5 -> the second row you are printing stars
505.66 -> from n minus 1 to n plus 1 right can we
510.22 -> write this n minus 1 as n minus 2 minus
512.95 -> 1 as 2 minus 1 is 1 therefore we would
515.65 -> be able to obtain one here why I am
517.69 -> writing 2 to actually relate this to the
520.63 -> second room this is n minus 2 minus 1
523.69 -> and this is n plus 2 minus 1 so we can
526.78 -> say that we are printing stars from n
528.76 -> minus 2 minus 1 to n plus 2 minus 1
531.7 -> similarly for the third row we can say
534.16 -> that we are printing stars from n minus
536.28 -> 3 minus 1 to n plus 3 minus 1 similarly
541.24 -> we can say that for the fourth row we
543.4 -> are printing stars from n minus 4 minus
545.68 -> 1 to n plus 4 minus 1 right and also for
550.39 -> the first row we are printing stars from
552.7 -> n minus 1 minus 1 to n plus 1 minus 1
556.9 -> which is equals to n only so what we are
559.96 -> actually obtaining from this particular
561.37 -> observation
564.08 -> when J is greater than or equals to n
567.26 -> minus I minus 1 and J is less than or
570.95 -> equals to n plus I minus 1 then only we
574.25 -> should print star otherwise we simply
576.56 -> print a blank space here I represents
579.56 -> the row suppose you are in the second
581.66 -> row then you should print stars from n
584 -> minus 1 to n plus 1 right this is the
590 -> idea we need to adopt as this is the
592.7 -> correct code we can add this in our
594.95 -> actual code inside this nested for loop
598.52 -> structure now you can add this whole
600.59 -> code snippet and it will definitely
603.5 -> print the pyramid as needed let's try to
607.52 -> execute the code and see whether we
609.08 -> would be able to get the pyramid or not
611.44 -> this is the same code that we had seen
614 -> previously the only changes that are
616.34 -> declared three variables and I and J
618.98 -> after that we have a printf function
621.14 -> that simply prints how many rows you
623.48 -> want in your pyramid and after that we
625.58 -> simply accept the input that is our
628.34 -> number of rows and then the same code is
630.77 -> used to print the pyramid according to
633.47 -> the number of rows let's see what could
635.54 -> be the output it asks us to how many
639.71 -> rows you want in your pyramid let's say
642.14 -> I entered 10 and hit enter as you can
646.94 -> see a pyramid is printed on the screen
649.39 -> with 10 Drew's I hope you got it thank
653.24 -> you for watching
654.96 -> [Applause]
658.01 -> [Music]
660.51 -> you
665.22 -> [Music]
Source: https://www.youtube.com/watch?v=KdM6OrvcjPI