Given a number N, the task is to generate the pyramid sequence pattern which contains N pyramids one after the other as shown in the examples below.
Examples:
Input: N = 3 Output: * * * *** *** *** *************** Input: N = 4 Output: * * * * *** *** *** *** ********************
Iterative Approach: The steps for an iterative approach to print the Mountain Sequence Pattern for a given number N:
- Run two nested loops.
- Outer loop will care for the row of the pattern.
- Inner loop will be caring for the column of the pattern.
- Take three variable k1, k2, and gap which helps in generating a pattern.
- After printing row of pattern update the value of k1 and k2 as:
- k1 = k1 + gap
- k2 = k2 + gap
Below is the implementation of the iterative approach:
C++
// C++ program for the above approach#include <iostream>using namespace std;// Function to create the mountain// sequence patternvoid printPatt(int n){ int k1 = 3; int k2 = 3; int gap = 5; // Outer loop to handle the row for (int i = 1; i <= 3; i++) { // Inner loop to handle the // Coloumn for (int j = 1; j <= (5 * n); j++) { if (j > k2 && i < 3) { k2 += gap; k1 += gap; } // Condition to print the // star in mountain pattern if (j >= k1 && j <= k2) { cout << "*"; } else { cout << " "; } } // Condition to adjust the value of // K1 and K2 for printing desire // Pattern if (i + 1 == 3) { k1 = 1; k2 = (5 * n); } else { k1 = 3; k2 = 3; k1--; k2++; } cout << endl; }}// Driver Codeint main(){ // Given Number N int N = 5; // Function call printPatt(N);} |
Java
// Java implementation of the above approachclass GFG{// Function to create the mountain// sequence patternstatic void printPatt(int n){ int k1 = 3; int k2 = 3; int gap = 5; // Outer loop to handle the row for(int i = 1; i <= 3; i++) { // Inner loop to handle the // Coloumn for(int j = 1; j <= (5 * n); j++) { if (j > k2 && i < 3) { k2 += gap; k1 += gap; } // Condition to print the // star in mountain pattern if (j >= k1 && j <= k2) { System.out.print("*"); } else { System.out.print(" "); } } // Condition to adjust the value of // K1 and K2 for printing desire // Pattern if (i + 1 == 3) { k1 = 1; k2 = (5 * n); } else { k1 = 3; k2 = 3; k1--; k2++; } System.out.println(); }} // Driver codepublic static void main (String[] args){ // Given Number N int N = 5; // Function call printPatt(N);}}// This code is contributed by Pratima Pandey |
Python3
# Python3 program for the above approach# Function to create the mountain# sequence patterndef printPatt(n): k1 = 3; k2 = 3; gap = 5; # Outer loop to handle the row for i in range(1, 4): # Inner loop to handle the # Coloumn for j in range(1, (5 * n) + 1): if (j > k2 and i < 3): k2 += gap; k1 += gap; # Condition to print the # star in mountain pattern if (j >= k1 and j <= k2): print("*", end = ""); else: print(" ", end = ""); print("\n", end = ""); # Condition to adjust the value of # K1 and K2 for printing desire # Pattern if (i + 1 == 3): k1 = 1; k2 = (5 * n); else: k1 = 3; k2 = 3; k1 -= 1; k2 += 1; print(end = ""); # Driver Code# Given Number NN = 5;# Function callprintPatt(N);# This code is contributed by Code_Mech |
C#
// C# implementation of the above approachusing System;class GFG{// Function to create the mountain// sequence patternstatic void printPatt(int n){ int k1 = 3; int k2 = 3; int gap = 5; // Outer loop to handle the row for(int i = 1; i <= 3; i++) { // Inner loop to handle the // Coloumn for(int j = 1; j <= (5 * n); j++) { if (j > k2 && i < 3) { k2 += gap; k1 += gap; } // Condition to print the // star in mountain pattern if (j >= k1 && j <= k2) { Console.Write("*"); } else { Console.Write(" "); } } // Condition to adjust the value of // K1 and K2 for printing desire // Pattern if (i + 1 == 3) { k1 = 1; k2 = (5 * n); } else { k1 = 3; k2 = 3; k1--; k2++; } Console.WriteLine(); }} // Driver codepublic static void Main (String[] args){ // Given Number N int N = 5; // Function call printPatt(N);}}// This code is contributed by shivanisinghss2110 |
* * * * * *** *** *** *** *** *************************
Time Complexity: O(N)
Recursive Approach: The pattern can be generated using Recursion. Below are the steps:
- Run two nested loops.
- Outer loop will care for the row of the pattern.
- Inner loop will be caring for the column of the pattern.
- Apart from these, variables K1, K2, and gap are needed.
- K1, K2 will cover the cases when the * is to be printed.
- Gap will cover the cases when spaces are to be printed.
- Recursively call the function fun(i, j + 1) for handling columns.
- Recursive call the function fun(i + 1, 0) for handling rows.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;int k1 = 2;int k2 = 2;int gap = 5;// Function to print pattern// recursivelyint printPattern( int i, int j, int n){ // Base Case if (j >= n) { k1 = 2; k2 = 2; k1--; k2++; if (i == 2) { k1 = 0; k2 = n - 1; } return 0; } // Condition to check row limit if (i >= 3) { return 1; } // Condition for assigning gaps if (j > k2) { k1 += gap; k2 += gap; } // Conditions to print * if (j >= k1 && j <= k2 || i == 2) { cout << "*"; } // Else print ' ' else { cout << " "; } // Recursive call for columns if (printPattern(i, j + 1, n) == 1) { return 1; } cout << endl; // Recursive call for rows return printPattern(i + 1, 0, n);}// Driver Codeint main(){ // Given Number N int N = 3; // Function Call printPattern(0, 0, N * 5); return 0;} |
Java
// Java program for the// above approachclass GFG{static int k1 = 2;static int k2 = 2;static int gap = 5; // Function to print pattern// recursivelypublic static int printPattern(int i, int j, int n){ // Base Case if (j >= n) { k1 = 2; k2 = 2; k1--; k2++; if (i == 2) { k1 = 0; k2 = n - 1; } return 0; } // Condition to check // row limit if (i >= 3) { return 1; } // Condition for assigning gaps if (j > k2) { k1 += gap; k2 += gap; } // Conditions to print * if (j >= k1 && j <= k2 || i == 2) { System.out.print("*"); } // Else print ' ' else { System.out.print(" "); } // Recursive call for columns if (printPattern(i, j + 1, n) == 1) { return 1; } System.out.println(); // Recursive call for rows return printPattern(i + 1, 0, n);}// Driver codepublic static void main(String[] args){ // Given Number N int N = 3; // Function Call printPattern(0, 0, N * 5);}}// This code is contributed by divyeshrabadiya07 |
Python3
# Python3 program for the# above approachk1 = 2k2 = 2gap = 5# Function to print pattern# recursivelydef printPattern(i, j, n): global k1 global k2 global gap # Base Case if(j >= n): k1 = 2 k2 = 2 k1 -= 1 k2 += 1 if(i == 2): k1 = 0 k2 = n - 1 return 0 # Condition to check row limit if(i >= 3): return 1 # Condition for assigning gaps if(j > k2): k1 += gap k2 += gap # Conditions to print * if(j >= k1 and j <= k2 or i == 2): print("*", end = "") # Else print ' ' else: print(" ", end = "") # Recursive call for columns if(printPattern(i, j + 1, n) == 1): return 1 print() # Recursive call for rows return (printPattern(i + 1, 0, n))# Driver Code# Given Number NN = 3# Function CallprintPattern(0, 0, N * 5)#This code is contributed by avanitrachhadiya2155 |
C#
// C# program for the// above approachusing System;using System.Collections.Generic;class GFG { static int k1 = 2; static int k2 = 2; static int gap = 5; // Function to print pattern // recursively static int printPattern(int i, int j, int n) { // Base Case if (j >= n) { k1 = 2; k2 = 2; k1--; k2++; if (i == 2) { k1 = 0; k2 = n - 1; } return 0; } // Condition to check // row limit if (i >= 3) { return 1; } // Condition for assigning gaps if (j > k2) { k1 += gap; k2 += gap; } // Conditions to print * if (j >= k1 && j <= k2 || i == 2) { Console.Write("*"); } // Else print ' ' else { Console.Write(" "); } // Recursive call for columns if (printPattern(i, j + 1, n) == 1) { return 1; } Console.WriteLine(); // Recursive call for rows return printPattern(i + 1, 0, n); } // Driver code static void Main() { // Given Number N int N = 3; // Function Call printPattern(0, 0, N * 5); }}// This code is contributed by divyeshrabadiya07 |
* * * *** *** *** ***************
Time Complexity: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer Geeks Classes Live


