Pattern 18

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 write code to print the pattern given in output format below



Input Format
A number n
Output Format
pat181
Question Video
Constraints
1 <= n <= 10
Also, n is odd.
Sample Input
7
Sample Output
*	*	*	*	*	*	*	
* *
* *
*
* * *
* * * * *
* * * * * * *


  • Editorial

    "Nobody values time more than a programmer."

    Question:

    1. You are given a number n.

    2. You have to write code to print the pattern given in the output format below.

    Input format:

    A number n

    Output format:

    Constraints:

    1 <= n <= 10;Also n is odd.

    Solution Approach:

    In the given problem, we have to form an hourglass shape pattern of stars.

    But before moving on to directly printing our required, let us first analyze how the printing would have been required if the hourglass did not have any hollow space (or the hourglass was filled completely with stars).

    For input value 'n' =7, let us analyze the completely filled hourglass pattern line-by-line:

    We observe there is a pattern between the spaces and stars with regards to the row number, as we have seen for a number of problems till now.

    So, we begin programming for printing the above shape and then on to our final objective:

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

    java false

    We are successfully able to print the following pattern.

    Now that we have the required shape, we just have to modify the content a little to reach our end goal.

    Notice that:

    To remove the required content from the  upper half of the pattern, we make the following changes:

    The above constitutes our final required solution code which yields the desired output.

    import java.util.*;
                                    public class Main{
                                        public static void main(String[] args)
                                        {
                                            Scanner scn = new Scanner(System.in);
                                            int n = scn.nextInt();
                                            int sp = 0;     // there are 0 spaces in first row
                                            int st = n;     // there are n stars in first row
                                    
                                            for(int i = 1;i<=n;i++)
                                            {
                                                for(int j=1;j<=sp;j++) // for printing spaces
                                                {
                                                    System.out.print("	");
                                                }
                                    
                                                for(int j =1;j<= st;j++)
                                                {
                                                    if( i > 1 && i<=n/2 && j>1 && j < st) //if the row is between middle row and first row
                                                    {                                   //if the star is not the first or last of the row
                                                        System.out.print("	");
                                                    } 
                                                    else
                                                    {
                                                        System.out.print("*	");            
                                                    }
                                                }
                                    
                                                if( i<= n/2)
                                                {
                                                    st -= 2;
                                                    sp++;
                                                }
                                                else
                                                {
                                                    st += 2;
                                                    sp--;
                                                }
                                    
                                                System.out.println();
                                            }
                                        }
                                    }

    java false

    The above constitutes our final required solution code which yields the desired output.

  • Asked in Companies
  • Related Topics






Video Solution

Code Solution

Run
 
Run
Id Name