# Pattern 20 #### Try First, Check Solution later

1. You should first read the question and watch the question video.
2. Think of a solution approach, then try and submit the question on editor tab.
3. We strongly advise you to watch the solution video for prescribed approach.

`1. You are given a number n.2. You've to write code to print the pattern given in output format below.`
Input Format
`A number n`
Output Format
` `
Question Video
Constraints
`1 <= n <= 10 Also, n is odd.`
Sample Input
`5`
Sample Output
`*				*	*				*	*		*		*	*	*		*	*	*				*	`

• Editorial

"Before a code can be reusable, it has to be usable."

Question:

1. You are given a number n.

2. You've to write code to print the pattern given in output format below.

Input format:

A number n

Output format:  Constraints:

1 <= n <= 10;Also n is odd.

Solution Approach:

We have to print a pattern that resembles the shape of 'W'.

In this design, we observe that we need stars in all the rows of the first and last column; and on the diagonal positions, provided diagonal positions are below the middle row (inclusive).

So, we know the condition for the first and last row will be 'j' == 1 || 'j' == 'n' and condition for diagonal values are 'i' == 'j' || 'i' +'j' == n + 1 But in diagonal, we place another check, that is , the middle row is crossed ( 'i' > n/2). Any place else, then we need to print whitespaces.

So implementing the above required conditions in programming:

```import java.util.*;
public class Main{
public static void main(String[] args)
{
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
for(int i = 1;i<=n;i++)
{
for(int j =1;j<=n;j++)
{
if( j == 1 || j == n)  //if first or last column
{
System.out.print("*	");
}
else if(i > n/2 && (i ==j || i +j == n + 1))   // part of either diagonal below middle row
{
System.out.print("*	");
}
else
{
System.out.print("	");
}
}
System.out.println();
}
}
}```

java false

Final output for input value 'n'= 5:  • Related Topics

Run

Run
Id Name