Pattern 1

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
pat11
Question Video
Constraints
1 <= n <= 100
Sample Input
2
Sample Output
*	
* *


  • Editorial

    "Finding patterns is the essence of wisdom."

    Solution Approach:

    Let us first understand what the given problem demands from the given output format.

    It shows that:

    1.We only have to print stars (*) in the given problem.

    2.We have to print the 'n' input number of rows.

    3.Additionally, we need to print the same number of stars in a row as the row number itself.

    4.Each star on the multiple star lines has a 'tab' of space between them (A tab is a combination of 4 spaces).

    We have to take care of all these parameters while figuring a solution for the given problem.

    Furthermore, it could be felt "intuitively" that this workload seems repetitive in nature. And where repetition is involved, we bring loops into the play.

    We first understand how the loop is going to play into the problem. Writing a normal loop and running it for a given number 'n' number of times, printing "Hello" in every iteration.

    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++)
                                    		{
                                    			System.out.println("Hello");
                                    		}
                                    	}
                                    }
      java  false  

    Output for the above code:

    So we were able to print a Hello for 'n' number of lines. So we can take the help of this concept and take a bit further to get out final desired result.

    We program the following code for the given problem.

    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<=i;j++)		//MAGIC LOOP
                                    			{
                                    				System.out.println("*");
                                    			}
                                    			System.out.println();	
                                    		}
                                    	}
                                    }
      java  false  

    But, for understanding purpose, we ignore the inner loop for the time being and call it magic as it is something magical for us about which, we have no knowledge except the fact that it performs perfectly for the input value fed into it.

    This magic is an important concept in the field of Software Engineering and is known as Abstraction.

    Abstraction is the process of extracting the underlying structures, patterns or properties of a mathematical concept, removing any dependence on real world objects with which it might originally have been connected, and generalizing it so that it has wider applications or matching among other abstract descriptions of equivalent phenomena.

    Abstraction is a fundamental concept in computer science and software development, and is similar in nature to the process of generalization.

    Abstraction means giving your thinking a layered overview. When working on a layer based or placed over another layer, we have faith that the layer related or below is going to work perfectly.

    Here, we do not question the working of the inner loop (labelled magic in illustration).We just focus on the outer loop with iterating variable 'i'.We have faith on our magic code that it is going to work.

    The thought process is that if magic is given an input value (i itself is the input value in the example in place) it will give out its output error-free. We have no concern as to how it produces the output.

    This process can be correlated with a construction of a house. A person hires three men; an electrician, a painter, and a partitioner.

    The Electrician is supposed to provide (create or enhance) electric points in the house for electric services in the house according to the wishes of the person.

    The Painter is supposed to  paint the house according to the wishes of the person.

    The Partitioner is supposed to partition the house into parts in accordance with wishes of the person.

    All three men work according to the wishes of the person in exchange for some currency.The person has no idea about how the work of the three men is executed or how they will do their work, he just knows that upon payment of certain amount of currency, he will get his desired specifications in his house. This is his faith (working on abstraction). If he was concerned with the working of every layer (each of the three men is work), he would not be able to get his job done properly (finished house).

    Drawing similiarity into our programming world, whenever a user executes a program and the program asks user for input, the user is not concerned as to how the program will execute, he is only concerned with the correct output. He has faith on the skills of the programmer that he has programmed a competent code which will provide him with required/desired output. The currency involved in executing a program is the time & memory which is required for execution.

    Coming back to our problem at hand, the outer loop we wrote has the duty to provide the inner loop proper values for which it prints correspondingly.

    The current written program displays this pattern as output, which is similar to what we desire, but not the same. As discussed earlier, we need to provide tabs in between the stars while printing them.

    To achieve this, we make amends to the print statement inside the inner loop and add a ' ' after printing each star.

    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<=i;j++)		//MAGIC LOOP
                                    			{
                                    				System.out.println("*	");
                                    			}
                                    			System.out.println();	
                                    		}
                                    	}
                                    }
      java  false  

    Now this solution is complete for the given problem,and provides us with the desired output.

    This output is produced when 5 is entered as input value.

    For a final analysis of the code:

    Remember, the outer loop is responsible for the number of rows that going to be printed and the number of stars to be printed in a row.

    The inner loop is responsible for the printing of the number of stars in a row in the correct manner (space adjustments included). Then control is returned to outer loop which changes the line for further printing of the inner loop, repeating for the required number of times.

  • Asked in Companies
  • Related Topics






Video Solution

Code Solution

Run
 
Run
Id Name