Y shaped pattern Last Updated : 26 Sep, 2022 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice Print a ‘Y’ shaped pattern from asterisks in N number of lines. Examples: Input: N = 12Output: * * * * * * * * * * * * * * * * * * * Input: 8Output: * * * * * * * * * * * * * 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-1Check if i > sTraverse a loop on j from 0 till s-1 and print " " i.e., spaceElse, Iterate on j from 0 till i-1 and print " " i.e., spaceThen print "*" i.e., asteriskIterate on k from 0 till 2*t-1 and print " " i.e., spaceDecrement 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. Comment More infoAdvertise with us Next Article Must Do Coding Questions - Topic-wise A akashjha2671 Follow Improve Article Tags : Pattern Searching DSA pattern-printing Practice Tags : Pattern Searchingpattern-printing Similar Reads Interview PreparationInterview Preparation For Software DevelopersMust Coding Questions - Company-wise Must Do Coding Questions - Topic-wiseCompany-wise Practice ProblemsCompany PreparationCompetitive ProgrammingSoftware Design-PatternsCompany-wise Interview ExperienceExperienced - Interview ExperiencesInternship - Interview ExperiencesPractice @GeeksforgeeksProblem of the DayTopic-wise PracticeDifficulty Level - SchoolDifficulty Level - BasicDifficulty Level - EasyDifficulty Level - MediumDifficulty Level - HardLeaderboard !!Explore More...Data StructuresArraysLinked ListStackQueueBinary TreeBinary Search TreeHeapHashingGraphAdvance Data StructuresMatrixStringAll Data StructuresAlgorithmsAnalysis of AlgorithmsSearching AlgorithmsSorting AlgorithmsPattern SearchingGeometric AlgorithmsMathematical AlgorithmsRandomized AlgorithmsGreedy AlgorithmsDynamic ProgrammingDivide & ConquerBacktrackingBranch & BoundAll AlgorithmsProgramming LanguagesCC++JavaPythonC#Go LangSQLPHPScalaPerlKotlinWeb TechnologiesHTMLCSSJavaScriptBootstrapTailwind CSSAngularJSReactJSjQueryNodeJSPHPWeb DesignWeb BrowserFile FormatsComputer Science SubjectsOperating SystemsDBMSComputer NetworkComputer Organization & ArchitectureTOCCompiler DesignDigital Elec. & Logic DesignSoftware EngineeringEngineering MathematicsData Science & MLComplete Data Science CourseData Science TutorialMachine Learning TutorialDeep Learning TutorialNLP TutorialMachine Learning ProjectsData Analysis TutorialTutorial LibraryPython TutorialDjango TutorialPandas TutorialKivy TutorialTkinter TutorialOpenCV TutorialSelenium TutorialGATE CSGATE CS NotesGate CornerPrevious Year GATE PapersLast Minute Notes (LMNs)Important Topic For GATE CSGATE CoursePrevious Year Paper: CS exams Like