Pattern 6

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


  • Editorial

    "Programming is the art of algorithm design and the craft of debugging errant code"

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

    Solution Approach:

    In this problem, we have to construct a hollow diamond using stars. This problem is not very different from the diamond pattern construction; just the stars have come on the outside. For 'n' = 5;


    We can see first there are 3 stars followed by a space, followed again by 3 stars.

    In this pattern, on each line, spaces increase or decrease by 2 in comparison to the line above it, and stars increment or decrement by 1.

    There is another stark difference in the diamond and hollow diamond pattern, and that is, whereas the diamond pattern was a square in shape, the hollow diamond is a rectangle, where the number of columns is two more than the number of rows.

    Programming for understanding the spacing and star printing concept:

    import java.util.*;
                                    public class Main{
                                     public static void main(String[] args)
                                     {
                                        Scanner scn = new Scanner(System.in);
                                        int n = scn.nextInt();
                                        int st = n/2 + 1,sp = 1; // sp = spaces ; st = stars
                                        for(int i =1; i<=n;i++)
                                        {
                                            System.out.println(st + "," + sp + "," + st);
                                            if( i<= n/2)
                                            {    sp += 2;      
                                                st--;
                                            }
                                            else
                                            {
                                                sp -= 2;
                                                st++;
                                            }    
                                        }
                                     }
                                    }

    java false

    And the output for the above code would be:


    This corresponds with the values for a number of stars, followed by the number of spaces; ultimately again followed by the number of stars required on a line.

    Now that we have understood the spacing and star printing conceptually, we modify this code to get our final submission ready code.

    Final Solution Code:

    import java.util.*;
                                    public class Main{
                                     public static void main(String[] args)
                                     {
                                        Scanner scn = new Scanner(System.in);
                                        int n = scn.nextInt();
                                        int st = n/2+1,sp = 1; // sp = spaces ; st = stars
                                        for(int i =1; i<=n;i++)
                                        {
                                            for(int j=1; j<= st;j++)        //printing stars
                                            {
                                                System.out.print("*	");
                                            }
                                            for(int j=1; j<= sp;j++)        //printing spacing
                                            {
                                                System.out.print("	");
                                            }
                                            for(int j=1; j<= st;j++)        //printing stars again
                                            {
                                                System.out.print("*	");
                                            }
                                            
                                            if( i<= n/2)
                                            {    st--;                  //decrementing stars in succeeding lines by 1
                                                sp+=2;                  //incrementing spacing in succeeding lines by 2
                                            }
                                            else
                                            {
                                                st++;                   //increasing stars in succeeding lines by 1
                                                sp-=2;                  //decreasing spacing in succeeding lines by 2
                                            }    
                                            System.out.println();       //changing to newline after one complete iteration of loop
                                        }
                                     }
                                    }

    java false

    Output(For input value of n = 5):

  • Asked in Companies
  • Related Topics






Video Solution

Code Solution

Run
 
Run
Id Name