Pattern 19

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
pat191
Question Video
Constraints
1 <= n <= 10
Also, n is odd.
Sample Input
3
Sample Output
*	*	*	
* * *
* * *


  • Editorial

    "Deleted code is debugged code!"

    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:

    We have to draw a "Swastika" pattern of stars.

    For n = 7


    Let us analyze this design line by line. We break this pattern into 5 components:

    We also observe that the components first and fifth and second and fourth; are symmetrically flipped about the middle column. So, it is but obvious that the conditions we will place for segregating the first and second components will be just flipped opposite for getting the fifth and fourth component respectively. In the third component, we just have to print stars for all the columns of the middle row (n/2 +1 row).

    The condition for First component will be: if( i == 1)

    Second component: else if(i <= n/2)

    Third component: else if( i == n/2 +1)

    Fourth component:else if(i < n)

    Fifth component: whatever is left constitutes the last row only.

    Looking into the first component:

    We need to print stars only till the middle column (n/2 +1) and the last column (n).

    Second component:

    We need to print stars only in the middle column (n/2 +1) and the last column (n).

    Third component:

    We need to print stars in the entire row for each column.


    Fourth Component:

    We need to print stars only in the first column (1) and middle column (n/2 +1).

    Fifth Component:

    We need to print stars from the middle column (n/2+1) till the end (n) and the first column (1) only.

    In each of the components, if the condition is met; Print stars, else print whitespaces.

    Programming Implementation for the same:

    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 <=n; j++)
                                                { 
                                                   if( i == 1)  //first component
                                                    {
                                                        if( j ==n || j <= n/2 + 1)
                                                        {
                                                            System.out.print("*	");
                                                        }
                                                        else
                                                        {
                                                            System.out.print("	");
                                                        }
                                                    }
                                                    else if(i <= n/2)   //second component
                                                    {
                                                        if( j == n || j == n/2 +1)
                                                        {
                                                            System.out.print("*	");
                                                        }
                                                        else
                                                        {
                                                            System.out.print("	");
                                                        }
                                                    }
                                                    else if( i == n/2 +1)   //third component
                                                    {
                                                        System.out.print("*	");
                                                    }
                                                    else if(i < n)  // fourth component
                                                    {
                                                        if(j == 1 || j == n/2 + 1)
                                                        {
                                                            System.out.print("*	");
                                                        }
                                                        else
                                                        {
                                                            System.out.print("	");
                                                        }
                                                    }
                                                    else    //fifth component
                                                    {
                                                        if(j ==1 || j>= n/2 + 1)
                                                        {
                                                            System.out.print("*	");
                                                        }
                                                        else
                                                        {
                                                            System.out.print("	");
                                                        }
                                                    }
                                                }
                                                System.out.println(); // changing row
                                            }
                                    
                                        }
                                    }

    java false

    Output for input value 'n' = 7:

  • Asked in Companies
  • Related Topics






Video Solution

Code Solution

Run
 
Run
Id Name