Pa ern problems
(Consider the n value is always 5 in this pdf only for understanding purpose)
No Input/Output Code Index representa on
1 Input: 5 import java.u l.*; 00 01 02 03 04
Output: public class Main{ * * * * *
***** public sta c void main(String [] args){ 10 11 12 13 14
***** Scanner sc=new Scanner([Link]); * * * * *
***** int n=[Link](); 20 21 22 23 23
***** for(int i=0;i<n;i++){ * * * * *
***** for(int j=0;j<n;j++){ 30 31 32 33 34
[Link]("* "); * * * * *
} 40 41 42 43 44
[Link](); * * * * *
}
}
}
Understand that the for loop can be used in any
method (which means in the end the first loop Output will be same but the index
should run n mes and the second loop should run n will be as follows:
mes to get a nxn square representa on as a result) 44 43 42 41 40
* * * * *
The below are the several types you can use: 34 33 32 31 30
Type 2: * * * * *
for(int i=n-1;i>=0;i--){ 24 23 22 21 20
for(int j=n-1;j>=0;j--){ * * * * *
[Link]("* "); 14 13 12 11 10
} * * * * *
[Link](); 04 03 02 01 00
} * * * * *
Output will be same but the index
Type 3: will be as follows:
for(int i=0;i<n;i++){ 04 03 02 01 00
for(int j=n-1;j>=0;j--){ * * * * *
[Link]("* "); 14 13 12 11 10
} * * * * *
[Link](); 24 23 22 21 20
} * * * * *
34 33 32 31 30
* * * * *
44 43 42 41 40
* * * * *
Type 4: Output will be same but the index
for(int i=n-1;i>=0;i--){ will be as follows:
for(int j=0;j<n;j++){ 40 41 42 43 44
[Link]("* "); * * * * *
} 30 31 32 33 34
[Link](); * * * * *
} 20 21 22 23 24
* * * * *
10 11 12 13 14
* * * * *
00 01 02 03 04
* * * * *
Note: These types should be known to solve certain
pa ern.
And here a er only opera ons (for loops) will be
provided in the code session.
2 Input: 5 for(int i=0;i<n;i++){ 00 01 02 03 04
Output: for(int j=0;j<n;j++){ *
* if(i<j){ 10 11 12 13 14
** [Link](" "); * *
*** 20 21 22 23 24
**** } * * *
***** else{ 30 31 32 33 34
[Link]("* "); * * * *
} 40 41 42 43 44
} * * * * *
[Link](); In the above index representa on
} White spaces are at the indexes:
01, 02, 03, 04,
12, 13, 14,
23, 24,
34
Derived understanding:
If(i<j) we need to print white space in
that spot.
3 Input: 5 for(int i=0;i<n;i++){ 00 01 02 03 04
Output: for(int j=0;j<n;j++){ *
* if(j<n-i-1){ 10 11 12 13 14
** [Link](" "); * *
*** } 20 21 22 23 24
**** else{ * * *
***** [Link]("* "); 30 31 32 33 34
} * * * *
} 40 41 42 43 44
[Link](); * * * * *
} In the above index representa on
White spaces are at the indexes:
00, 01, 02, 03,
10, 11, 12,
20, 21,
30
Derived understanding:
If(j<n-i-1) we need to print white
space in that spot.
4 Input: 5 for(int i=0;i<n;i++){ 00 01 02 03 04
Output: for(int j=0;j<n;j++){ * * * * *
***** if(j<i){ 10 11 12 13 14
**** [Link](" "); * * * *
*** } 20 21 22 23 24
** else{ * * *
* [Link]("* "); 30 31 32 33 34
} * *
} 40 41 42 43 44
[Link](); *
} In the above index representa on
White spaces are at the indexes:
10
20, 21
30, 31, 32
40, 41, 42, 43
Derived understanding:
If(j<i) we need to print white space
in that spot.
5 Input: 5 for(int i=0;i<n;i++){ 00 01 02 03 04
Output: for(int j=0;j<n;j++){ * * * * *
***** if(j>=n-i){ 10 11 12 13 14
**** [Link](" "); * * * *
*** } 20 21 22 23 24
** else{ * * *
* [Link]("* "); 30 31 32 33 34
} * *
} 40 41 42 43 44
[Link](); *
} In the above index representa on
White spaces are at the indexes:
14
23, 24
32, 33, 34
41, 42, 43, 44
Derived understanding:
If(j>=n-i) we need to print white
space in that spot.
6 Input: 5 for(int i=0;i<n;i++){ 00 01 02 03 04
Output: for(int j=0;j<n;j++){ * * * * *
***** if(i>0 && i<n-1 && j>0 && j<n-1){ 10 11 12 13 14
* * [Link](" "); * *
* * } 20 21 22 23 24
* * else{ * *
***** [Link]("* "); 30 31 32 33 34
} * *
} 40 41 42 43 44
[Link](); * * * * *
} In the above index representa on
White spaces are at the indexes:
11, 12, 13
21, 22 ,23
31, 32 ,33
Derived understanding:
If(i>0 && i<n-1 && j>0 && j<n-1) we
need to print white space in that
spot.
Li le Advanced Pa ern
7 Input: 5 for(int i=0;i<n;i++){ 00 01 02 03 04 05 06 07 08
Output: for(int j=0;j<2*n-1;j++){ *
* if(j<(n-1)-i || j>(n-1)+i){ 10 11 12 13 14 15 16 17 18
*** [Link](" "); * * *
***** } 20 21 22 23 24 25 26 27 28
******* else{ * * * * *
********* [Link]("* "); 30 31 32 33 34 35 36 37 38
} * * * * * * *
Note: } 40 41 42 43 44 45 46 47 48
Here its not a [Link](); * * * * * * * * *
nxn } In the above index representa on
representa on White spaces are at the indexes:
rather a 00, 01, 02, 03, 05, 06, 07, 08
n x (2n-1) 10, 11, 12, 16, 17, 18,
representa on 20, 21 ,27, 28,
To create a 30,38
perfect pyramid Derived understanding:
structure If(j<(n-1)-i || j>(n-1)+i) we need to print white
space in that spot.
Input: 5 for(int i=0;i<n;i++){ 00 01 02 03 04 05 06 07 08
8 Output: for(int j=0;j<2*n-1;j++){ * * * * * * * * *
********* if(j<i || j>=(2*n-1)-i){ 10 11 12 13 14 15 16 17 18
******* [Link](" "); * * * * * * *
***** } 20 21 22 23 24 25 26 27 28
*** else{ * * * * *
* [Link]("* "); 30 31 32 33 34 35 36 37 38
} * * *
Note: } 40 41 42 43 44 45 46 47 48
Here its not a [Link](); *
nxn } In the above index representa on
representa on White spaces are at the indexes:
rather a 10,18
n x (2n-1) 20, 21 ,27, 28,
representa on 30, 31, 32, 36, 37, 38,
To create a 40, 41, 42, 43, 45, 46, 47, 48
perfect pyramid Derived understanding:
structure If(j<i || j>=(2n-1)-i) we need to print white space
in that spot.