02 October 2018 - Krishna Lede

Printing Pyramid Pattern in C

In this video lesson we will see how we can create star pyramid pattern in C using nested loops. So, lets begin! Wish you happy learning! spartakrish, ...

[Music]

hello everyone welcome to my channel in today's video lesson we are going to create this pattern so in this pattern we have Phi rows and columns are varying from 1 to 5 the red line on the left indicate the number of spaces from the left side of the stars so that we can form the pattern like pyramid but this pattern is different from the pattern we have seen in the last video so let's get started double click on turbo C icon on the desktop to start turbo C IDE is open now select new from the file menu to create new file first of all I am going to draw our pattern in common and from one to five okay so let's begin void main opening race in this program we need two variables which controls the inner and outer loop say I and chain and we need extra variable to control the spaces in this pattern so spaces this is the third variable CLRS here now going to write external for loop for I equal to 1 I less than or equal to 5 I plus controls number of rows in our okay now inner loop but before writing inner loop we need one extra loop which controls these spaces in the pattern first of all

more spaces are there consider we have ten spaces in between star and left side and gradually the spaces are in decreasing order that is if we have ten spaces in the first line then in the second line we have nine and in the third line we have eight and in the fourth line we have seven and so on so four spaces for species equal to one this is less than or equal to 50 minus i spaces less plus space okay now printing starts for J equal to 1 J less than or equal to I this is the variable because we have varying spaces from 1 to 5 in rows so say printf star one space okay after printing a row we need program is finished now let's see it saving the program name it has pattern 3c compile the program to make sure that everything is okay I have nine program is okay run the program and you can see the desired output on the screen spaces are more so we can control them from this loop say 30 - I control of 9 okay or we can say 35 - I I think it's okay now so it's time to go through it let us see how our program works [Applause]

which controls basis okay so in the value of I equal to 1 we have 35 - I spaces in first line so 34 spaces 34 spaces on the screen that is 35 you - I but the value of I each one in this case thirty four spaces inner loop will print only one stuff because the inner loop is converted into for J equal to 1 J less than or equal to i but the value of I is 1 here so J equal to 1 J less than equal to 1 J plus plus so definitely this loop will execute one time so it will print only one star along with spaces see here instead of spaces if we write another character like dot then we can easily see how spaces controls the stars and form the pyramid pattern so let's have a look instead of printing this space I am writing dot here and rerun the program so you can see number of varying dots from Row 1 to Row 4 you in Row 1 more dots are there and in route route 2 there are less dot then now again after completion of loop the value of I will be 2 in the outer loop draw 33 spaces on the screen that is 35 minus 2 or loop is converted into 4 J equal to 1 J less than equal to I but the value of eyes to now so this loop will bring 2 stars on

the screen in this way for I equal to 3 the program draws 32 spaces on the screen that is 35 you minus 3 and the Scarlet is converted into J equal to 1 J less than equal to 3 so it will print 3 stars on the screen in this way for I equal to 4 and 4 I 1205 you brought T 1 spaces that is hard if I you - for the loop is converted into 4 J equal to 1 J less than equal to 4 so we have 4 stars on the screen and at last for I equal to 4 you it draws 30 spaces on the screen that is 35 you - 530 this loop which converted into 4 J equal to 1 J less than or equal to y UJ plus bliss so we have 5 stars in this line and when the value of I equal to 6 the program terminals so this is how our program works that's all for this video lesson if you have any question or suggestions regarding this video please let me know in the comment section below in the next video lesson we will see how we can create number pattern in C till then bye thanks for watching you

XML Transcript: