# Pattern 2 #### 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 create a pattern of * and separated by tab as shown in output format.`
Input Format
`A number n`
Output Format
` `
Question Video
Constraints
`1 <= n <= 100`
Sample Input
`5`
Sample Output
`*	*	*	*	*	*	*	*	*	*	*	*	*	*	*`

• Editorial

"There are only two ways to write an error-free program, Only the third one works!"

Question:

1. You are given a number n.

2. You've to create a pattern of * and separated it by tab as shown in the output format.

Input format:

A number n

Output format:

* * * * *

* * * *

* * *

* *

*

Constraints:

1 <= n <= 100

Solution Approach:

In the given problem, we need an "n" number of stars in the first row and one less in the preceding rows.

Let us analyze a familiar code:

for(int j =1;j<=i;j++)

{

System.out.print("*"); //magic

}

This "magic" loop will print the number of stars equal to the value of i.

That is, if i = 2, there will be 2 iterations and 2 stars will be printed, one in each iteration.

We need to have "faith" that the value of "i" that will be provided to this loop, it will print that many stars for us.

Now since this resolves the printing of stars part of our problem, we put our focus on the spacing of these stars aspect of the problem.

Given to us in the output format, we see that the problem expects us to print the stars in different "n" lines each containing "n" number of stars will n reduce in each preceding line by 1.

Furthermore, between each star, it requires us to put a tab space ( 4 spaces together), which is solved in the "magic" loop itself.

Now we work on giving the magic loop (which will be our inner loop), the values of "i" through an outer loop, which provides the iteration conditions for the inner loop.  The inner loop prints the number of stars in a row with proper spacing, but the outer loop provides the termination condition of the inner loop as the number of stars required on the particular line.

For the above example, we take n = 5 and then dry run the process we require.

Programming for the same will be done as follows:

The illustration below showcases the inner loop and the spacing of each star.

```import java.util.*;

public class Main{
public static void main(String[] args)
{
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();

int nstars = n;
for(int j = 1;j<=nstars; j++)
{
System.out.print("*	");
}
}
}```

java false

Writing the outer loop and thus concluding the programming implementation:

```import java.util.*;

public class Main{
public static void main(String[] args)
{
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();

int nstars = n;

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

java false

Abstraction is a very important concept used for understanding how we approach the solution. We wrote the inner loop first, defining its functioning and having complete faith that it will work without failure. Then we nest it inside an outer loop, which uses it according to convenience, having faith that upon giving input to the inner loop, the inner loop will always produce the correct output.

It is this layered programming methodology, which helped us solve this problem, and this modeling of solution for implementation in the programming language is known as Abstraction.

• Related Topics

Run

Run
Id Name