Patterns (Using JAVA)
Pattern 1
1
22
333
4444
Logic
- The pattern has rows equal to the highest number in the pattern (Example 4 in our case). So the loop will run up to the highest number in the pattern.
- Each row has the same number of columns as the number present in it. So other loop will run the same number of times as the row number. (Example 1st row for only one time where as 4th row four times)
- Now the last step in pattern is printing it. We can see that for each row we have to print the exact same row number, which constitute to our 1st step (i.e 1st loop for row) so we will print the 1st loop variable.
Click Link to see Pattern 1
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Pattern 2
1
12
123
1234
Logic
- The pattern has rows equal to the highest number in the pattern (Example 4 in our case). So the loop will run up to the highest number in the pattern.
- Each row has the same number of columns as the number present in it. So other loop will run the same number of times as the row number. (Example 1st row for only one time where as 4th row four times)
- Now the last step in pattern is printing it. We can see that for each row we have to print all the numbers from 1 to the row number, which constitutes to our 2nd step ( i.e. how many times each row will run), So we will print 2nd loop vaariable
Click Link to see Pattern 2
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Pattern 3
1
23
456
Logic
- The pattern has 3 rows so 1st loop will run 3 times.
- Each row has to run the same numbers of columns present in it (Example 1st row has 1 column so it will run only 1 time 2nd row has 2 columns so it will run 2 times)
- Now the last step is printing the pattern. As we can see that there is no relation we can infer form the rows and columns, so we take another variable just to print the pattern.
Click Link to see Pattern 3
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Pattern 4
4444
333
22
1
Logic
- The pattern has 4 rows so 1st loop will run 4 times.
- The pattern is similar to Pattern1 the difference is that it is just the reverse of it. So the loop is just opposite of it.
- Now the printing of pattern. As we can see it prints constant digits for each row so we will use the variable of the 1st loop.
Click Link to see Pattern 4
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Pattern 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Logic
- The pattern has 5 rows so the 1st loop will run 5 times
- As we can see there are white spaces present before every row. We will have a seperate loop for printing white spaces.
- Now as we can see the white spaces are on every row therefor the loop for white space will be within 1st loop more over form observation we can see that white spaces are decreasing with every row, so we need to decrease the white spaces accordingly on every row.
- As we can observe each row runs for the same number of times as the row number (Example 1st row for 1 time 3rd row for three times). So the next loop will run as the same time the row number.
- Now for printing we observe that the value for every row varies on every row so we will use the inner most loop variable for printing. More over we can observe that there is a space after every number printed so we will concatenate a space with the variable printed
Click Link to see Pattern 5
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Comments
Post a Comment