0% found this document useful (0 votes)
2 views

Pattern Program

Uploaded by

rajb99633
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Pattern Program

Uploaded by

rajb99633
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Tips:

 Visualize the pattern


 Divide into rows and columns

Pattern:
Steps

1.

2. Divide into rows and columns.


3. Watch rows and columns both are increasing.

4. Create and maintain row and column table (i and j)


5. Maintain row column values.
6. That’s all we are set to write logic and program now.

7. First for loop for Rows always.

Second for loop for Columns.


8. Mistake:

9. Let’s analyse our logic.


When i= 1 and j=1

When i= 1 and j=2


But we need one * only in first row !!!

That’s why j value will always depend on i


10. Fresh Start with fresh logic.
11. When When i= 1 and j=1, prints 1 *

now When i= 1 and now j=2, inner condition fails, cursor moves next line.

now When i= 2 and j=1


Condition true, prints one more star in second row now.

now When i= 2 and j=2, condition true , one more * in second row.
now When i= 2 and j=3, condition fails, breaks inner loops, cursor moves to 3 rd
row
now.

now When i= 3 and j=1 for new iteration


Prints one * in 3rd row now.

now When i= 3 and j=2, condition true , one more * in 3 rd


row

now When i= 3 and j=3condition true, one more * in 3 rd


row
now When i= 3 and j=4, condition false , cursor moves to new line and i=4 now.
Same way it will print 4 stars
That’s it.

Time for Another Pattern then -


Pattern:

Steps
 Divide into row and columns.

 Here if you see row i remains same, only column changes.


 Only a little change is needed to previous program.

Let’s see the changes.

 No change for the row loop


 Watch the table

row is same, no change , column starts from 4 now and slowly decreases.

 J=4 to j>=1

 Let’s see the logic


 i=1 and j=4 , enter loop

 Condition true, print one * in 1st row


 j=3 now , decrements

 Condition true one more * in 1st row


 j=2 now , condition true , one more * in 1st row
 j=1 , condition true , one more * in 1st row
 j=0 condition false inner loop breaks, cursor moves down

Cursor moves to new Line

 now i=2 and j=4 for new iteration


 condition true now , prints one * in 2nd row

 j=3 now , condition true


 one more * in 2nd row
 i=2 and j=3 true one more * in 2nd row
 i=2 and j=2 true one more * in 2nd row

 i=2 and j=1 now condition is false, cursor moves to new line.
 i=3 and j=4 again

Prints two *

 Same way finally one *


 Summary

Notes

 Use nested for loop.


 i value is always constant, number of rows top to bottom.
 2nd for loop may or may not depend on value of i
 This program j depends on i
 First use only print()
 Then use println()

You might also like