Pattern Question ##5
2 min readDec 5, 2022
QUESTION1:
21. 1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
SOLUTION:
public class Hello{
public static void main(String[] args) {
Pattern(5);
}
static void Pattern(int n){
int count = 1;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= i; j++){
System.out.print(count + " ");
count++;
}
System.out.println();
}
}
}
QUESTION2:
22. 1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
SOLUTION:
public class Hello{
public static void main(String[] args) {
Pattern(5);
}
static void Pattern(int n){
boolean temp = true;
for(int i = 1; i <= n; i++){
temp = i%2 == 0 ? false : true;
for(int j = 1; j <= i; j++){
if(temp){
System.out.print(1 + " ");
temp = false;
}else{
System.out.print(0 + " ");
temp = true;
}
}
System.out.println();
}
}
}
QUESTION3:
24. * *
** **
* * * *
* * * *
* ** *
* ** *
* * * *
* * * *
** **
* *
SOLUTION:
public class Patterns {
public static void main(String[] args) {
Pattern(5);
}
static void Pattern(int n){
for(int i = 1; i <= 2*n; i++){
int c1 = i <= n ? i : 2*n-i+1;
int c2 = i <= n ? 2*n-i+1 : 2*n-c1+1;
for(int j = 1; j <= 2*n; j++){
if(j == 1 || j == c1 || j == 2*n || j == c2){
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
}
QUESTION4:
25. *****
* *
* *
* *
*****
SOLUTION:
public class Patterns {
public static void main(String[] args) {
Pattern(5);
}
static void Pattern(int n){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n-i; j++){
System.out.print(" ");
}
for(int k = 1; k <= n; k++){
if(i == 1 || i == n || k == 1 || k == n){
System.out.print("*");
}else{
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.