C Pattern Printing Programs Explained | using One Logic to create More than 10 Patterns

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to learning lad in this video we will see how we can use one simple logic and print more than ten patterns using that one logic first we will start with a basic pattern and I'm going to explain the logic behind the creation of that pattern and then we will see how we can construct the program for that and after that we will modify the program and we will keep on displaying more and more patterns so here we will begin with this right angle triangle star pattern where we have the stars aligned in such a way that it looks like a right angle triangle now if we divide this pattern in terms of rows and columns then this is the first row second row third row and the fourth row and if we divide it in terms of columns then first column second column third column and fourth column if you look at this pattern closely then the number of columns printed in each row is actually equal to row number for example if we look at the second row then row number is two and the number of columns displayed in this row are two and similarly if you look at the row four we have four columns containing this star displayed in the fourth row so we can say that the number of columns that we have to display for a particular row is equal to row number and by using this logic we will construct the program so here I have already written some code I have included D stdio dot H header file so that we can use the functions like printf scanf etcetera and then we have this main function which is the entry point of our program from our perspective so here the first thing that we do in this program is we will declare the variables that we are going to use here in this program we will ask the user about four how many roads he wants the pattern so to store that user input we need a variable and I'm gonna use integer type and I'm going to call it as num rows for number of rows and then as I said before we will print this part in terms of rows and columns so we need a couple of variables for storing the row and column information so I'm gonna call them as row and column now the next thing that we do is we will ask the user about the number of rows for which he wants a pattern so we will use the printf function in here and we will say enter the number of rows and then we will read the input from the user using this kind of function and we will store that in this num rows variable so it's going to be num rows okay now we know for how many rows we have to dissipate this pattern so let's use a couple of for loops and display the pattern so the first for loop that we use is for printing every row so here I'm gonna initialize the row variable with a value of 1 and the condition here will be how long we want to execute this for loop as long as this row variable contains a value less than or equal to the value present in the number of variable we want to run this for loop that is if the user wants Phi rows then this for loop has to run five times and after that with every iteration we will increment the value of this row variable okay now here if I print out the value of this row variable then it will print out the row number so percentage D let's add a new line in here and print out the row variables value now if I save this program and if I run this code into the number of rows I'm gonna enter five and you guys can see it will provide us five rows now for every row here we need to display whatever the columns that we have to display so here what we do is we will use another for loop for printing the columns for a particular row so in a row we have seen that we need more than one column so we will use a for loop in here and this for loop is for columns so we will initialize the column variable with a value of 1 and the next thing is how many columns that we want in a particular row and we know that that value is the row number and here we have this row number available in this row variable so we want to run this for loop for the columns as long as this column variable contains a value which is less than or equal to row this row variable contains the 0 number and after printing one column we will increment the value of this column variable so in this inner for loop that we have four columns will provide us the row number of columns for a particular row and here we can specify what we want to print in the column so I'm gonna use the printf statement in here and I'm gonna specify the character that I want to print that yes star and after that I want to add a space in here so that it can look nice okay now the next thing is this for loop this inner for loop that we have it will print whatever number of columns that we have to display for a particular row so after printing the columns in a particular row to print the next row we have to go to the next line so we want every row in separate separate lines so after this inner for loop we come out of the inner for loop and in this for loop which we have for printing the rows we will use a printf statement and we will insert a new line in here now if you are confused about why we need this printf statement and why we need to insert this new line then just remove this line this printf statement and run your code you will come to know about it okay now we have written the program let's run this enter the number of rows I'm going to say file rows now you guys can see if we have this pattern displayed this is a right angle triangle pattern and this pattern contains the star that we wanted to display now the next thing that we do here is we will start modify this core and we will display different different patterns now the first thing that I'm gonna do is I'm gonna copy this for loop and I'm gonna paste it in here okay and after that I'm gonna comment out the first power look that we have I mean the VAR loop which printed the pattern for us so now we have this duplicate copy of this previous code now here we have seen that our pattern was in the right angle triangle shape and what if we wanted to reverse that pattern so we can make a slight modification in here that is the row variable was starting from 1 now we will make it start from num rows so num rows okay and then what we want to do how long we want to continue this for loop as long as this row variable contains a value which is greater than or equal to 1 we will run this for loop and with each iteration we will decrement the value of the row variable if I save this program and if I run this code into the number of rows I'm gonna say 5 you guys can see we have reversed this pattern just by making a couple of modification to our code now the next thing that we can do is we can combine these two code and we can complete that pattern I'm going to remove these comments if I say oh this program now and if I run this code enter the number of rows 5 so it will display this pattern now of course the number of rows that we aren't entering in here is actually 4 in the upper half of this pattern but since we are modifying the code you know it will display more than fire-roast but if you want you can modify that code also now in this pattern we have one problem and that is in the center of this pattern we can see that we have two rows containing same number of columns or same number of stars but in order for this pattern to look nice I think we should have only one row which would have maximum number of columns so what we can do is we can make this four loop which is the second half of this pattern to start from number of rows minus one now if I save this program and if I run this code enter the number of poles and if I enter PI now you guys can see we have only one row which is containing maximum number of columns and this pattern now looks a bit complete okay now till now we have displayed the star or any other character that we wanted to display in here now what if we want to display the numbers we can also do that so first I'm going to comment out this bottom half of this pattern and here instead of displaying the star we will display a number for example we can display the row number so I'm gonna refer the row variable in here I'm gonna sell the code I'm gonna run the program I'm gonna say Phi Rose now you guys can see we have row number displayed in each row and if you want you can change this one to column now the program will display the column number for example why Rose nervous can see it will display the column number one two three four and five now if you want you can activate the lower half also so lower half of the pattern I'm going to copy this printf statement and I'm gonna paste it in here and with this subtle modification if I save the program and run the code if I enter the number of rows as five now you guys can see we have this pattern displayed here we are displaying only column number or their own number if you want we can display the row number and also the column number so I'm gonna use another placeholder in here % 8d and we will display the row number and the column number together so copy this code paste it in here save the program run it into the number of roses file you guys can see we have this pattern displayed now and here also if you want we can modify the row and column various we can swap them so we will display the column variables value first and then we will display the row variables value so here also we will do the same thing and I'm gonna save the program run the code number of rows as five now you guys can see we have this pattern by making simple modifications we can display more and more patterns till now we have displayed the Stars or any character or the numbers in here what if we want to display the alphabetical characters so in C programming language whenever we use a character we use the ASCII value if you know the ASCII value of a character then you can display the character associated with that ASCII value for example for capital yeh the ASCII value is 65 so here in this program if I use the format specifier as percentage D and if I provide 65 in here here also I'm gonna do it in the bottom half of the pattern and if I save the program and run the core if I enter fire rows now you guys can see the alphabetical character a displayed in here that's because the ASCII value of this capital a is 65 and here we are instructing the printer function to display the character associated with the ASCII value 65 so heel in that pattern that we have seen we have the capital e a displayed in all the columns or wherever we were displaying something we were displaying the capital a now what if we want every first column of the pattern to display capital A then the second column to display capital B the third column to display a capital C and like that so we can make that modification and for that I'm going to use an integer variable in here I'm going to call it as C it give any name and I will initialize this with a value of 65 this 65 is the ASCII value of this alphabetical character yay so here we have to write CH and then after displaying the alphabetical character then we have to move to the next alphabetical character the ASCII value for the alphabetical character is capital e a capital B capital C they are in the ascending order that is 60 F is the ASCII value of the capital e a 66 is the ASCII value of the capital B 67 is the ASCII value of the capital C and like that so what we can do is here in this printf function we can write CH plus column ok and here also we will do the same thing so it will be CH plus column I will use the parentheses to group the code okay now here what we are doing is while printing the first column we will perform CH plus column variables value we will display that character the column variables value for every iteration will be changing starting from 1 so I'm gonna save this program and if I run this code into the number of rows I'm gonna say Phi rows now you guys can see we have this pattern where the alphabetical characters are in ascending order in this pattern but it is starting from B not yay and that is because here we have initialized the CH variable with 65 and while printing we are performing CH plus column variables value this column variables value is starting from 1 so the first time it tries to print it will be 65 plus 1 which will be 66 which is the ASCII value of the character capital B so in order to start from capital a we will store 64 in this CH variable now if I save this program and if I run the code into the number of rows I'm gonna say Phi rows now you guys can see this pattern is starting from a now the next thing that we see is what happens if we add the row variables value in here ch+ row so let's make that modification and let's see what happens so I'm going to make CH plus row in both these cases save the program run the core I'm gonna say Phi rows and you guys can see we have this pattern so every row is containing the same alphabetical character now what if we want the alphabetical characters in continuous order in the pattern for example in the first row we have one one column so we want a in there in the second row we want to B then C then D EF like that so we can modify this program and we can display that so here what we have to do is we have to increment the value of this yet variable after displaying it every time so here I'm using the increment operator so the CH variables value will be used first and then it will be incremented so here we will initialize this CH with 65 here now so 65 will be used first and then it will be incremented so that it can print the next alphabetical character next time okay we will do the same thing in here also now if I save the program and run this code into the number of rows I'm gonna enter 5 now you guys can see we have the alphabetical characters in continue order ABCD efg like that and here also you can make a lot of modifications now let's take a look at the one last pattern that we see for this tutorial and that is how we can display the numbers in continuous order in the pattern so the number will start from 1 then it will display 2 3 4 5 6 and like that so here I'm gonna initialize this CH variable with a value of 1 and after that here with this printf function we want to display the number not the character so we would use the format specifier as percentage D now okay after making this modification we can save the program and we can run the code enter the number of rows I'm gonna save file now you guys can see we have this pattern in here where the numbers are in continuous order so starting from one but in this pattern if we look at it closely then for the numbers from 1 to 9 we can display them in one character space but after that from number 10 onwards we need two character space to display them because there are two letters in that number so what we can do is we can tell the printf function to format or display the numbers with two character space and here we can make the multiplication as percentage 0 to D that means I want to display the number in to character space and for any number if two character spaces are not needed then include a 0 with that so here also we will do 0 to D okay after making this modification let's run the code number of roses file now you guys can see we have 0 displayed for the numbers from 1 to 9 because you know for them to character space was not needed but since we have mentioned to include 0 whenever two character space is not needed it has included that now what if you want to display the numbers in two character space and you don't want to include that zero so all you have to do is you have to get rid of this 0 in here percentage 2d okay save the program run the code and you guys can see now two character space is used for printing the numbers and whenever the two character space is not needed a blank space is included so this is it guys this is how you guys can use one simple logic and you can construct more than 10 patterns using C programming language if you like the content then hit the like button if you don't like it hit it is like button if you want to say something then there that in the comment box for more tutorials like this do subscribe to the channel thank you for watching I'll see you later in the next video
Info
Channel: LearningLad
Views: 78,337
Rating: 4.9365358 out of 5
Keywords: Pattern Printing, C Pattern Printing, C Pattern Printing Programs, C Star Pattern Printing, Computer Programming, Programming for Beginners
Id: KNud5vUursw
Channel Id: undefined
Length: 20min 4sec (1204 seconds)
Published: Sun Jan 05 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.