How to solve any Star Pattern Program

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends in this video we will learn how to solve any star pattern program you might have come across many videos playlists or lengthy blogs explaining different pattern programs so do you need to spend hours learning each of them answer is no what you need is to understand the basics and learn the tricks so that you can answer any random pattern question which is given to you now if you go through this video you will be able to make any of these patterns shown and i will explain to you how we have used java language over here but if you know c plus plus you can also watch it as concept wise it is the same now you might have played with blocks when you were a kid you could create any kind of shapes and designs from a simple block we are going to use the same method here what we are going to learn is two basic building blocks these are how to make an increasing triangle and a decreasing triangle using just these two we will teach you how to join them together to make any kind of pattern that is asked so a complex pattern is nothing but these two basic triangles either filled with star hash or dollar an important point you need to know before you start is that when we print we do it row by row you always start printing from left side of the screen if your star is printed away from the left side of the screen or there is space in between the stars like in this two hill pattern then you do not jump and print this is actually done by printing spaces so in this line there are four spaces printed first then one star and then again 7 spaces and then 1 star and so on for all remaining rows we cannot go back to previous row so you have to print this entire row and then move to next row so let's get started the first thing in pattern is the size size means how big or how many rows you want in the triangle now either you could be asked to write a main and take size from the user here you can use any input class like scanner to take this input or if you are writing a function this n could be passed to you as a parameter or if you are given size in question itself in that case you can just directly assign it to a variable n for our examples we will consider that we have size in variable n which has value 5. also in the remaining videos we will just focus on the code to make the pattern i will not write main every time as i assume that you will know how to just put the code inside any main or function block and compile it second point is that the pattern programs require you to use loops if you do not know basics of for loop please first watch my video on iterations let's start with a simple square as an example first now if you are asked to print one star you will write a print statement which will print a star for you now if you are asked to print n stars what will you do we will use a loop here the loop will start from j is equal to 1 to j less than equal to m this loop will run five times and print the star five times now for the loop to run five times there are these two ways and you need to stick to only one method out of these one is where you start from one and you run this loop till less than and equal to n other is you start from zero then run till less than n both will run five times if we start from zero it will run the loop zero one two three four times which means it runs five times and if you start from one then j will take values one two three four five which also runs five times the reason i'm focusing on this is that i see many times kids confusing between less than and less than equal to and it messes up the whole pattern so pick one which you are comfortable with and stick with it we will use 1 to less than equal to n for all our examples so we have written this code with which we can print one row but what we want is to print this row five times to get a square pattern so what i will do is insert this complete set of code inside another loop which will print this row 5 times but if you run this you will see all stars are getting printed on the same row how do we fix this we do know that print statement prints in same rows and println statement goes to the next line so can we just change the print star statement to println if we do that what happens is now each star is printed in next line what we want is 5 stars in same rows and then it should go to next line means this loop will continue to have print statement and only after this whole row is printed or the whole loop is executed we want to go to next line so we will insert a println statement as the last statement of the outside loop so let's just understand how the loop will work we start with the outer loop it holds our row numbers and starts with 1 then we enter the inner loop it holds our column numbers and starts with 1 and runs till 5 printing all the columns once inner loop is finished it executes the println statement to go to next row the control comes back to outer loop and outer loop increments by 1 to row 2. again inner loop prints 5 columns as it will again start from 1 to 5. then the println statement takes it to the beginning of next row this repeats for all rows till i reaches 5 and then we exit from the outer loop the key points which you need to remember from this program is do not forget to put println statement and it should be the last statement of the outer loop this is very important point and if you miss writing println statement before the closing bracket your whole pattern can go for a toss now if i just run this code you will see your output has stars too close and it is giving an effect of rectangle instead of square you can fix this by just adding an extra space next to it in the print statement so this is our final code to print square let's go back to the print statement we are printing star here this could actually be anything if you print hash it will be a pattern of cash or a dollar then it will be a pattern of dollar and so on now let's learn our first basic pattern increasing triangle let's take our previous program which printed a square we will just edit this to print triangle we now know that the outer loop determines the rows and inner loop determines the columns we see that in this pattern the rows are 5 or n only so our outer loop is correct already and requires no change it is already printing end rows from 1 to n coming to columns instead of printing 5 stars or printing 1 to n every time it should print 1 star for first row then two star for second row then three star for third row and so on so the point to note here is that actually my row number is telling me every time how many stars to print row number is stored in variable i which is starting from 1 and incrementing to 2 3 4 till all rows are printed so what we will just change here is set the inner loop to run from 1 to i this will ensure inner root runs for only one time in the first row 2 times in the second row then 3 then 4 till n this completes our program for increasing triangle so important takeaway from this program is to make an increasing triangle outer loop is set from 1 to n and nested loop is set from 1 to i you need to remember this the remaining program is exactly the same now let's learn second basic pattern decreasing triangle we will again take a square program we will edit this to print decreasing triangle here again if you see this pattern outer loop is correct already and requires no change and it is already printing n rows from 1 to n but now we need the columns to print decreasing numbers of stars 5 4 3 2 1. how do we change our nested for loop to reduce by 1 every time this time we will change the start condition we will use i as the start condition so for the first row loop will run from 1 to 5 printing 5 stars second row it will run from 2 to 5 that means 4 times and then 3 2 and 1 star this completes our program for decreasing triangle so here what you need to remember is that to make decreasing triangle pattern the outer loop is from 1 to n and the nested loop is from i to n remaining program is exactly the same now you have the basic ammunition in place if you do not understand these two basic triangles go back rewind and understand it again let's take a look at some of the other patterns we saw earlier all of the patterns you see here are made from combination of these two type of triangles as you know we always start printing from left side of the screen so where we see blank it's actually the same triangle pattern made with spaces so this pattern is made up of two triangles one increasing pattern of space and one decreasing pattern of star this pattern also has two triangles one is decreasing pattern of face followed by one increasing pattern of star this pattern has three triangles first it has a decreasing pattern of space followed by increasing pattern of star and one more increasing pattern of star also do note in all of these here the rows are n only but column have different printing options like space or star so let's try to solve them let's pick up this pattern first this pattern has two triangles first is a decreasing triangle printing spaces and second is increasing triangle printing stance do note that on same rows we have to now print space as well as star let's write the program for it since our number of rows are same as that of square pattern we will first copy the outer loop from our previous program and it runs from 1 to 5. do note our println line which takes us to the new row every time sticks to the end of the outer loop and you should not forget it or link it with the any of the inside loop we next recall our code for nested loop of decreasing triangle and we will write our nested for loop for decreasing triangle from i to n here the print statement will print spaces only now the next triangle is increasing triangle pattern printing stars in the same row so we will just add another nested for loop for increasing triangle pattern this loop as we know will run from 1 to i this will print star let's take a look at how this will work for the first row when i is equal to 1 first the upper nested loop will execute and print 5 spaces then in the same row it will enter the second nested loop and will print one star then it will go to the next row and i will become two then it will print four spaces from the first nested loop and then two stars from the second nested loop so these two nested loops are working one after the other to print all columns we need in a row this will go on till the entire pattern is printed now this is the program for this pattern you need to remember two important points here one println statement is linked to rows or outer loop we do not repeat it with every inner loop second if you are printing a star with one space in the end that means two characters in your print statement then your spaces also have to be always two so basically the number of characters which we are going to use in our print statement have to be the same otherwise you will see a distorted image this is an important point and you should always remember to print same number of characters what if the pattern was like this it is an increasing triangle of space and then decreasing triangle of star since our number of rows are same as that of square pattern we will first copy the same outer loop from our previous program and it runs from 1 to 5. since first loop is increasing triangle we will write our nested for loop for increasing triangle from 1 to i here the print statement will print space only now the next triangle is decreasing triangle so we will just add another nested loop for decreasing triangle pattern which will run from i to n this will print star this is the final code for this pattern now what if pattern was like this a hill pattern this pattern has one decreasing triangle of space then two increasing triangle of star now we already have written code for first two triangles of this pattern here the third triangle is also an increasing triangle pattern so we will write one more for loop inside the same outer loop now there is a small catch here which you need to remember let's run this and see here when you see the output you see it does not have a peak why because our loop knows how to make five columns each in such patterns actually there is one less column printed so to make this nested loop run one less we will just change the for condition of first increasing triangle from less than equal to to less than this will make this loop run one less time so as good as printing one less column this is our final code for this pattern now what if it was a reverse mountain pattern here there is one increasing triangle and two decreasing triangle now we already have written code for first two triangles of this pattern like in previous question we will just add a third nested loop for increasing triangle and to make sure we have our valley correct we will have nested loop print one less column this is our code for reverse mountain pattern let's look at pattern which doubles up on rows as well now let's take this diamond pattern this has double the number of rows so what are we going to do here you have to print a pattern below another pattern we already have a code to make the hill pattern and for inverted hill pattern 2. here note that the rows are double so what we need to do is just place two code one below the other along with the outer loop so you run one outer loop along with its nested loop for rows of upper set of triangles and run another outer loop along with its nested loop for rows of lower set of triangles let's run this to see if by just doing this we get the desired pattern this is close but this two has one issue that it does not have pointed edges looking closely we see actually our rows two don't totally double up there is one less row printed to give us a pointed edge the solution is similar to what we did for columns we need to print one less row how do we do that we just reduce the condition of outer for loop to print one less row effectively deleting the last row so we will just change our outer for loop condition to less than instead of less than equal to this is our required program for printing and diamond you can now try any different patterns like these shown using the same logic break it into increasing and decreasing triangles and then just bundle up the code hope you have understood how to solve the pattern programs in our next video we will cover how to do number patterns if you have any doubts do reach out to us at simplycoding.in thank you and goodbye [Music]
Info
Channel: Simply Coding
Views: 86,596
Rating: 4.9284182 out of 5
Keywords: Simply Coding, simplycoding, CBSE, ICSE, SSC, Computer Applications, Computer Science, Information technology, NCERT, start pattern program in java, diamond pattern, hill pattern, triangle pattern
Id: xzstcj3Cuso
Channel Id: undefined
Length: 18min 47sec (1127 seconds)
Published: Mon Nov 09 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.