Pattern Questions ##6

Palakkgoyal
2 min readDec 8, 2022

QUESTION1:

26.   1 1 1 1 1 1
2 2 2 2 2
3 3 3 3
4 4 4
5 5
6

SOLUTION:

public class Patterns {
public static void main(String[] args) {
Pattern(6);
}
static void Pattern(int n){
for(int i = 1; i <= n; i++){

for(int j = 0; j <= n-i; j++){
System.out.print(i + " ");
}
System.out.println();
}
}

}

QUESTION2:

27.   1 2 3 4  17 18 19 20
5 6 7 14 15 16
8 9 12 13
10 11

SOLUTION:

public class Patterns {
public static void main(String[] args) {
Pattern(4);
}
static void Pattern(int n){
int c1 = 1;
int c2 = 4*n + 1;

for(int i = 1; i <= n; i++){

for(int j = 1; j < i; j++){
System.out.print(" ");
}

int c3 = c2;

for(int j = 1; j <= (n-i+1)*2; j++){
if(j <= n-i+1){
System.out.print(c1 + " ");
c1++;
}else{
System.out.print(c3 + " ");
c3++;
}
}
System.out.println();

c2 -= (n-i);
}
}

}

QUESTION3:

28.      *
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

SOLUTION:

public class Patterns {
public static void main(String[] args) {
Pattern(5);
}
static void Pattern(int n){
//run the outer loop as many times as the number of rows
for(int i = 1; i < 2*n; i++){

int c = i <= n ? i : 2*n - i;

for(int j = 1; j <= n-c; j++){
System.out.print(" ");
}

for(int k = 1; k <= c; k++){
System.out.print("* ");
}
System.out.println();
}
}
}

QUESTION4:

30.         1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5

SOLUTION:

public class Patterns {
public static void main(String[] args) {
Pattern(5);
}
static void Pattern(int n){
//run the outer loop as many times as the number of rows
for(int i = 1; i <= n; i++){

//print spaces
for(int j = 1; j <= n-i; j++){
System.out.print(" ");
}

//print rhs of pattern
for(int k = i; k >= 1; k--){
System.out.print(k);
}

//print lhs of pattern
for(int l = 2; l <= i; l++){
System.out.print(l);
}
System.out.println();
}
}
}

Thank you for reading. If you have any queries then, please let me know in the comment section. I will surely be responsive toward it.

--

--

Palakkgoyal

Solutions to all your coding related problems at one point. DSA question on daily basis and much more.