Pattern 10

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
pat101
Question Video
Constraints
1 <= n <= 100
Also, n is odd.
Sample Input
5
Sample Output
		*	
* *
* *
* *
*


  • Editorial

    "To understand is to perceive patterns!"

    Question:

    1. You are given a number n.

    2. You have 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; Also n is odd

    Solution Approach:

    The given pattern is a sort of hollow diamond, which can be called a parallelogram.

    In this problem, we encounter two types of spacing;

    1. Outer Spacing

    2. Inner Spacing

    The outer spacing is responsible for the whitespaces outside the design. The inner spacing is responsible for the whitespaces inside the design pattern.

    Analyzing a sample design for a value of n =5:


    Here, we can see the outer spacing decreases and then increases. Also, outer spacing is initialized from a value half of n (n/2). The inner space on the first line does not exist, but then increases by an added factor of 2 in succeeding iterations and then decreases by the same factor as outer spacing increases.

    We initialize inner space to be -1 because it is the same thing as 0, meaning no space to be printed, but -1 here will serve a purpose as it will help us in increasing and decreasing inner spaces by 2.

    Let us understand the spacing and star printing by writing a program; like we usually do, for printing the number of spaces and stars required on each line.

    import java.util.*;
                                    public class Main{
                                        public static void main(String[] args)
                                        {
                                            Scanner scn = new Scanner(System.in);
                                            int n = scn.nextInt();
                                            int os = n/2;       //outer spacing = half of n
                                            int is = -1;        //same as 0; not to print anything
                                    
                                            for(int i = 1;i <= n; i++)
                                            {
                                                System.out.println(os +","+ is);
                                    
                                                if(i <= n/2)
                                                {
                                                    os--;
                                                    is += 2;
                                                }
                                                else
                                                {
                                                    os++;
                                                    is -= 2;
                                                }
                                            }
                                    
                                        }
                                    }
      java  false  

    This program yields the following output:


    This represents the number of outer spaces and inner spaces needed on each line respectively.

    import java.util.*;
                                    public class Main{
                                        public static void main(String[] args)
                                        {
                                            Scanner scn = new Scanner(System.in);
                                            int n = scn.nextInt();
                                            int os = n/2;       //outer spacing = half of n
                                            int is = -1;        //same as 0; not to print anything
                                    
                                            for(int i = 1;i <= n; i++)
                                            {
                                                for(int j =1;j<= os;j++)        //print outer spaces
                                                {
                                                    System.out.print("	");
                                                }
                                                System.out.print("*	");          //print star
                                                
                                                for(int j = 1;j<=is;j++)
                                                {
                                                    System.out.print("	");
                                                }
                                    
                                                //now we address the special condition
                                                //as we print only a single star on the first
                                                //and last row without any inner space
                                    
                                                if( i > 1 && i < n) //if the current row is neither first or last
                                                {
                                                    System.out.print("*");
                                                }
                                    
                                                if(i <= n/2)    
                                                {
                                                    os--;
                                                    is += 2;
                                                }
                                                else
                                                {
                                                    os++;
                                                    is -= 2;
                                                }
                                                System.out.println();
                                            }
                                    }
                                    }
      java  false  

    The above code gives us the correct output. (Given below, for instance value n = 7)

  • Asked in Companies
  • Related Topics






Video Solution

Code Solution

Run
 
Run
Id Name