# Pattern 17 #### 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

"Creativeness is finding patterns where none exist!"

Question:

1. You are given a number n.

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

Input format:

A number n

Output format:  Constraints:

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

Solution Approach:

Analyzing the given required pattern line-by-line:  The given shape resembles an arrow. It consists of n/2 spaces in each row except the special row (the middle row) where instead of those spaces, we print stars.

Focusing on the arrowhead, the stars first increase till the middle row and then decrease. Increment and decrement take place by the difference of 1.

We first program for the arrowhead construction:

```import java.util.*;
public class Main{
public static void main(String[] args)
{
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int sp = n/2;  //variable to store space count
int st = 1;     //variable to store star count
for(int i = 1;i<=n;i++)
{
for(int j =1; j<=sp ; j++)  //printing whitespaces
{
System.out.print("	");
}
for(int j =1 ;j<=st;j++)    // printing the stars
{
System.out.print("*	");
}
if( i <= n/2)       //checking if less than or equal to middle row
{
st++;       //increasing stars till middle row
}
else{
st--;       //decreasing stars post middle row
}
System.out.println(); //Changing the row
}
}
}```

java false

This will yield us the arrowhead required:  Now we move forward to the tail of the arrow.

The condition is simple: instead of printing whitespaces in the middle row, we print the same number of stars as the number of spaces.

The modification in the code is made as follows; completing our code:

```import java.util.*;
public class Main{
public static void main(String[] args)
{
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int sp = n/2;  //variable to store space count
int st = 1;     //variable to store star count
for(int i = 1;i<=n;i++)
{
for(int j =1; j<=sp ; j++)  //printing whitespaces
{
if( i == n/2+1)         //checking middle row
{
System.out.print("*	");
}
else
{
System.out.print("	");
}
}
for(int j =1 ;j<=st;j++)    // printing the stars
{
System.out.print("*	");
}
if( i <= n/2)       //checking if less than or equal to middle row
{
st++;       //increasing stars till middle row
}
else{
st--;       //decreasing stars post middle row
}
System.out.println(); //Changing the row
}
}
}```

java false

Output:  • Related Topics

Run

Run
Id Name