Open In App

Y shaped pattern

Last Updated : 26 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Print a ‘Y’ shaped pattern from asterisks in N number of lines.

Examples:

Input: N = 12
Output:

*             *
 *           *
  *         *
   *       *
    *     *
     *   *
      * *
       *
       *
       *
       *
       *
 

Input: 8
Output

*         *
 *       *
  *     *
   *   *
    * *
     *
     *
     *

Approach:

Follow the steps to solve this problem:

  • Initialize two variable s = N / 2 and t = N / 2.
  • Traverse a loop on i from 0 till N-1
  • Check if i > s
    • Traverse a loop on j from 0 till s-1 and print ” ” i.e., space
  • Else,  
    • Iterate on j from 0 till i-1 and print ” ” i.e., space
    • Then print “*” i.e., asterisk
    • Iterate on k from 0 till 2*t-1 and print ” ” i.e., space
    • Decrement t by 1.
  • Print ” *” at the end of each iteration.

Below is the implementation of the above approach:

C++




// C++ code for the above approach
 
#include <iostream>
using namespace std;
 
int main()
{
 
    // Given integer N
    int N = 12;
 
    // Initialize s and t
    int s = N / 2;
    int t = s;
 
    // Traverse from 0 till N
    for (int i = 0; i < N; i++) {
 
        if (i > s) {
 
            for (int j = 0; j < s; j++)
                cout << " ";
        }
        else {
 
            for (int j = 0; j < i; j++)
                cout << " ";
 
            cout << "*";
 
            for (int k = 0; k < 2 * t; k++)
                cout << " ";
 
            // Decrement t
            t--;
        }
 
        cout << " *" << endl;
    }
 
    return 0;
}


Java




// Java code for the above approach
import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        // Given integer N
        int N = 12;
 
        // Initialize s and t
        int s = N / 2;
        int t = s;
 
        // Traverse from 0 till N
        for (int i = 0; i < N; i++) {
 
            if (i > s) {
 
                for (int j = 0; j < s; j++)
                    System.out.print(" ");
            }
            else {
 
                for (int j = 0; j < i; j++)
                    System.out.print(" ");
 
                System.out.print("*");
 
                for (int k = 0; k < 2 * t; k++)
                    System.out.print(" ");
 
                // Decrement t
                t--;
            }
 
            System.out.println(" *");
        }
    }
}
 
// This code is contributed by Rohit Pradhan


Python3




# Given integer N
N = 12
 
# Initialize s and t
s = N / 2
t = s
 
# Traverse from 0 till N
for i in range(0, N):
 
  if (i > s):
    for j in range(0, int(s)):
        print(" ", end = "")
  else:
    for j in range(0, i):
        print(" ", end = "")
 
    print("*", end = "")
     
    for k in range(0, (2*int(t))):
        print(" ", end = "")
 
    # Decrement t
    t = t - 1
 
  print(" *")
   
 # This code is contributed by akashish__


C#




// C# code to implement the approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
  public static void Main()
  {
     
    // Given integer N
    int N = 12;
 
    // Initialize s and t
    int s = N / 2;
    int t = s;
 
    // Traverse from 0 till N
    for (int i = 0; i < N; i++) {
 
      if (i > s) {
 
        for (int j = 0; j < s; j++)
          Console.Write(" ");
      }
      else {
 
        for (int j = 0; j < i; j++)
          Console.Write(" ");
 
        Console.Write("*");
 
        for (int k = 0; k < 2 * t; k++)
          Console.Write(" ");
 
        // Decrement t
        t--;
      }
 
      Console.WriteLine(" *");
    }
  }
}
 
// This code is contributed by code_hunt.


Javascript




<script>
 
// JavaScript implementation of the approach
 
// Given integer N
        let N = 12;
 
        // Initialize s and t
        let s = Math.floor(N / 2);
        let t = s;
 
        // Traverse from 0 till N
        for (let i = 0; i < N; i++) {
 
            if (i > s) {
 
                for (let j = 0; j < s; j++)
                    document.write(" ");
            }
            else {
 
                for (let j = 0; j < i; j++)
                    document.write(" ");
 
                document.write("*");
 
                for (let k = 0; k < 2 * t; k++)
                    document.write(" ");
 
                // Decrement t
                t--;
            }
 
            document.write(" *" + "<br/>");
        }
 
// This code is contributed by sanjoy_62.
</script>


Output

*             *
 *           *
  *         *
   *       *
    *     *
     *   *
      * *
       *
       *
       *
       *
       *

Time Complexity: O(N2), for using nested loops.
Auxiliary Space: O(1), as constant extra space is required.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads