Pattern 3

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
pat31
Question Video
Constraints
1 <= n <= 10
Sample Input
5
Sample Output
				*	
* *
* * *
* * * *
* * * * *


  • Editorial

    "Everything is a pattern!"

    Question:

    1. You are given a number n.

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

    Input format:

    A number n

    Output format:

    *

    * *

    * * *

    * * * *

    * * * * *

    Constraints:

    1 <= n <= 100

    Solution Approach:

    The required output format line by line can be represented as:

    For achieving the desired result, we take the help of a familiar 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-1,st = 1; // sp = spaces ; st = stars
                                            for(int i =1; i<=n;i++)
                                            {
                                                System.out.println(sp + "," + st);
                                                sp--;
                                                st++;
                                            }
                                        }
                                    }

    java false

    We declare sp for spaces (tabs) required and st for stars required on a particular line.

    For an input value of n =5, the program will yield the following output:

    Now analyzing this output, we can see that it gives numbers resembling the above illustration which shows us what we required in each line, in terms of spacing and star printing.

    We draw a memory diagram to showcase the relation between the RAM and console output:

    Now, we have to make certain adjustments to our code to make our output look like this:

    Instead of printing the number of spaces, we print the spaces and number of stars, the stars are printed.

    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-1,st = 1; // sp = spaces ; st = stars
                                            for(int i =1; i<=n;i++)
                                            {
                                               for(int j = 1;j<=sp;j++)     // runs till the number of spaces on particular line
                                               {                            
                                                   System.out.print("	");  //prints the number of spaces required on the line
                                               }
                                               for(int j = 1;j<=st;j++)    // runs till the number of stars on particular line
                                               {
                                                   System.out.print("*	"); //prints the number of stars required on the line
                                               }
                                                sp--;                      //decrementing the spaces by 1 for next line     
                                                st++;                      //incrementing the stars by 1 for next line
                                                System.out.println();      //Adding new line for output on console
                                            }
                                        }
                                    }

    java false

    The above program yields the desired output. For example for input value n = 5;

  • Asked in Companies
  • Related Topics






Video Solution

Code Solution

Run
 
Run
Id Name