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.
- The outer loop will care for the row of the pattern.
- The 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 the row of the 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 // Column 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 approach 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 // Column 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 code public 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 # Column 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 approach using 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 // Column 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 code public static void Main (String[] args) { // Given Number N int N = 5; // Function call printPatt(N);} } // This code is contributed by shivanisinghss2110 |
Javascript
<!-- Javascript program for the above approach. --><script>// Function to create the mountain// sequence patternfunction printPatt( n){ var k1 = 3; var k2 = 3; var gap = 5; // Outer loop to handle the row for(let i = 1; i <= 3; i++) { // Inner loop to handle the // Column for(let 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) { document.write("*"); } else { document.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++; } document.write("<br>"); }}//Driver Codevar N=3;printPatt(N);</script><!-- This code in contributed by nirajgusain5 --> |
* * * * * *** *** *** *** *** *************************
Time Complexity: O(N)
Auxiliary Space: O(1)
Recursive Approach: The pattern can be generated using Recursion. Below are the steps:
- Run two nested loops.
- The outer loop will care for the row of the pattern.
- The inner loop will be caring for the column of the pattern.
- Apart from these, variables K1, K2, and gap are needed.
- K1 and K2 will cover the cases when the * is to be printed.
- The 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
import java.io.*;// Java program for the// above approachclass GFG { static int k1 = 2; static int k2 = 2; static int gap = 5; // Function to print pattern // recursively public 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 code public 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 approach k1 = 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 Call printPattern(0, 0, N * 5)#This code is contributed by avanitrachhadiya2155 |
C#
// C# program for the // above approach using 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 |
Javascript
// Javascript program for the above approach let k1 = 2;let k2 = 2;let gap = 5;// Function to print pattern// recursivelyfunction printPattern(i, j, 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.log("*"); } // Else print ' ' else { console.log("\xa0\xa0"); } // Recursive call for columns if (printPattern(i, j + 1, n) == 1) { return 1; } console.log("<br>"); // Recursive call for rows return printPattern(i + 1, 0, n);}// Driver Code // Given Number N let N = 3; // Function Call printPattern(0, 0, N * 5); // This code is contributed by Pushpesh Raj. |
* * * *** *** *** ***************
Time Complexity: O(N)
Auxiliary Space: O(N) for call stack
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

… [Trackback]
[…] Here you will find 23878 more Info to that Topic: geeksforgeeks.org/mountain-sequence-pattern/ […]