Pattern Questions ##2

Palakkgoyal
2 min readNov 30, 2022

--

QUESTION1:

6.       *
**
***
****
*****

SOLUTION:

public class Hello{
public static void main(String[] args) {
Patterns(5);
}

public static void Patterns(int n){
//run the outer loop same as the number of rows
for(int i = 0; i < n; i++){

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

for(int k = 0; k <= i; k++){
System.out.print("*");
}

System.out.println();
}

}
}

QUESTION2:

7.   *****
****
***
**
*

SOLUTION:

public class Hello{
public static void main(String[] args) {
Patterns(5);
}

public static void Patterns(int n){
//run the outer loop same as the number of rows
for(int i = 0; i < n; i++){

//Print spaces
for(int j = 0; j < i ; j++){
System.out.print(" ");
}

//Print *
for(int k = 0; k < n-i; k++){
System.out.print("*");
}

//Print next line
System.out.println();
}

}
}

QUESTION3:

8.      *
***
*****
*******
*********

SOLUTION:

public class Hello{
public static void main(String[] args) {
Patterns(5);
}

public static void Patterns(int n){
//run the outer loop same as the number of rows
for(int i = 0; i < n; i++){

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

for(int k = 0; k <= i; k++){
System.out.print("*");
}

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

System.out.println();
}

}
}

QUESTION4:

9.  *********
*******
*****
***
*

SOLUTION:

public class Hello{
public static void main(String[] args) {
Patterns(5);
}

public static void Patterns(int n){
//run the outer loop same as the number of rows
for(int i = 0; i < n; i++){

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

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

for(int l = 1; l <= n-i-1; l++){
System.out.print("*");
}

System.out.println();
}

}
}

QUESTION5:

10.      *
* *
* * *
* * * *
* * * * *

SOLUTION:

public class Hello{
public static void main(String[] args) {
Patterns(5);
}

public static void Patterns(int n){
//run the outer loop same as the number of rows
for(int i = 0; i < n; i++){

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

for(int k = 0; k <= i; k++){
System.out.print("* ");
}

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
Palakkgoyal

Written by Palakkgoyal

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

No responses yet