Pattern 9

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


  • Editorial

    "Art is pattern informed by sensibility!"

    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:

    This problem is a combination of the two problems we have already faced, a diagonal of stars that leans down from left to right (Pattern 7) and a diagonal of stars that rise up from left to right (Pattern 8).

    So we combine the printing conditions of both the loops by an "or" operator to create the required program to print the "Cross of stars". We use the "or" operator so that the star is printed if either of the conditions is true.


    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++)      //decides the number of rows for output printing
                                            {
                                                for(int j =1;j<=n;j++)  //prints the stars in the row
                                                {
                                                    if(i == j || i + j == n + 1)        
                                                        {
                                                            System.out.print("*	");
                                                        }
                                                    else                        
                                                        System.out.print("	");    
                                                }
                                                System.out.println();   //changes the row on output console
                                            }
                                        }
                                    }

    java false

    Output for input value n = 5:

  • Asked in Companies
  • Related Topics






Video Solution

Code Solution

Run
 
Run
Id Name