Print Triangle separated pattern - GeeksforGeeks

21 Jan, 2022
Given a number N, the task is to print the triangle separated pattern. 
Triangle Separated Pattern: Pattern in which four triangles (left, down, right, up) are separated by forward and backward slash, see this below: \*****/ *\***/* **\*/** ***/*** **/*\** */***\* /*****\  
Note: N should be an odd number and the value of N should be more than 4.Examples:  
Input: N = 5
Output: 
        \***/
        *\*/*
        **/**
        */*\*
        /***\

Input: N = 7
Output:
        \*****/
        *\***/*
        **\*/**
        ***/***
        **/*\**
        */***\*
        /*****\
 
Approach: By observing the above pattern, when the indexes of rows and columns are equal, then ” is printed and when the sum of indexes of rows and columns is N, then ‘/’ is printed. Below is the recursive approach: 
Use two value i for rows and j for column, which iterates from (0, 0) to (N-1, N-1) for printing the require pattern.Recursively iterates from (0, 0) to (N-1, N-1): Base Case: If indexes of rows and columns are greater than or equal to N is the termination condition for the given pattern.  
Use two value i for rows and j for column, which iterates from (0, 0) to (N-1, N-1) for printing the require pattern.
Recursively iterates from (0, 0) to (N-1, N-1): Base Case: If indexes of rows and columns are greater than or equal to N is the termination condition for the given pattern.  
Base Case: If indexes of rows and columns are greater than or equal to N is the termination condition for the given pattern.  
if(i >= N) {
  return 0;
}
if(j >= N) {
  return 1;
}
 
Print Statement: If the base case condition is not met, then print ‘/’, ” and ‘*’ on the basis of below conditions:  
if(i==j) {
   print('')
}
else if(i + j == N-1) {
   print('/')
}
else {
   print('*')
}
 
Recursive Call: At each recursive call(except the base case), return the recursive function for next iteration for rows and column:  
// Recursive call for rows
recursive_function(i, j+1, N)

// Recursive call for changing rows
recursive_function(i+1, j, N)
 
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ program to print the triangle// separated pattern using// star and slash character #include <bits/stdc++.h>using namespace std; // Function to print pattern recursivelyint printPattern(    int i, int j, int n){    // Base Case    if (j >= n) {        return 0;    }    if (i >= n) {        return 1;    }     // Conditions to print slash    if (j == i || j == n - 1 - i) {         // Condition to print        // forward slash        if (i == n - 1 - j) {            cout << "/";        }         // Condition to print        // backward slash        else {            cout << "\\";        }    }     // Else print '*'    else {        cout << "*";    }     // Recursive call for rows    if (printPattern(i, j + 1, n)        == 1) {        return 1;    }     cout << endl;     // Recursive call for changing    // the rows    return printPattern(i + 1, 0, n);} // Driver Codeint main(){    int N = 9;     // Function Call    printPattern(0, 0, N);     return 0;}
// Java program to print the triangle// separated pattern using// star and slash characterclass GFG{  // Function to print pattern recursivelystatic int printPattern(    int i, int j, int n){    // Base Case    if (j >= n) {        return 0;    }    if (i >= n) {        return 1;    }      // Conditions to print slash    if (j == i || j == n - 1 - i) {          // Condition to print        // forward slash        if (i == n - 1 - j) {            System.out.print("/");        }          // Condition to print        // backward slash        else {            System.out.print("\\");        }    }      // Else print '*'    else {        System.out.print("*");    }      // Recursive call for rows    if (printPattern(i, j + 1, n)        == 1) {        return 1;    }      System.out.println();      // Recursive call for changing    // the rows    return printPattern(i + 1, 0, n);}  // Driver Codepublic static void main(String[] args){    int N = 9;      // Function Call    printPattern(0, 0, N);}} // This code is contributed by Rajput-Ji
# Python 3 program to print the triangle# separated pattern using# star and slash character # Function to print pattern recursivelydef printPattern(i,j, n):     # Base Case    if (j >= n) :        return 0    if (i >= n):        return 1     # Conditions to print slash    if (j == i or j == n - 1 - i):         # Condition to print        # forward slash        if (i == n - 1 - j):            print("/",end="")         # Condition to print        # backward slash        else:            print("\\",end="")     # Else print '*'    else:        print("*",end="")     # Recursive call for rows    if (printPattern(i, j + 1, n)        == 1):        return 1     print()     # Recursive call for changing    # the rows    return printPattern(i + 1, 0, n) # Driver Codeif __name__ == "__main__":     N = 9     # Function Call    printPattern(0, 0, N) # This code is contributed by chitranayal
// C# program to print the triangle// separated pattern using// star and slash characterusing System; class GFG{   // Function to print pattern recursivelystatic int printPattern(    int i, int j, int n){    // Base Case    if (j >= n) {        return 0;    }    if (i >= n) {        return 1;    }       // Conditions to print slash    if (j == i || j == n - 1 - i) {           // Condition to print        // forward slash        if (i == n - 1 - j) {            Console.Write("/");        }           // Condition to print        // backward slash        else {            Console.Write("\\");        }    }       // Else print '*'    else {        Console.Write("*");    }       // Recursive call for rows    if (printPattern(i, j + 1, n)        == 1) {        return 1;    }       Console.WriteLine();       // Recursive call for changing    // the rows    return printPattern(i + 1, 0, n);}   // Driver Codepublic static void Main(String[] args){    int N = 9;       // Function Call    printPattern(0, 0, N);}} // This code is contributed by Rajput-Ji
<script>// Javascript program to print the triangle// separated pattern using// star and slash character // Function to print pattern recursivelyfunction printPattern(i,j,n){    // Base Case    if (j >= n) {        return 0;    }    if (i >= n) {        return 1;    }        // Conditions to print slash    if (j == i || j == n - 1 - i) {            // Condition to print        // forward slash        if (i == n - 1 - j) {            document.write("/");        }            // Condition to print        // backward slash        else {            document.write("\\");        }    }        // Else print '*'    else {        document.write("*");    }        // Recursive call for rows    if (printPattern(i, j + 1, n)        == 1) {        return 1;    }        document.write("<br>");        // Recursive call for changing    // the rows    return printPattern(i + 1, 0, n);} // Driver Code let N = 9;// Function CallprintPattern(0, 0, N);  // This code is contributed by avanitrachhadiya2155</script>
\*******/
*\*****/*
**\***/**
***\*/***
****/****
***/*\***
**/***\**
*/*****\*
/*******\
 
ukasp
Rajput-Ji
nidhi_biet
avanitrachhadiya2155
simmytarika5
pattern-printing
Pattern Searching
Recursion
School Programming
pattern-printing
Recursion
Pattern Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Pattern Searching using Suffix Tree
Check if a string contains uppercase, lowercase, special characters and numeric values
Finite Automata algorithm for Pattern Searching
Anagram Substring Search (Or Search for all permutations)
How to validate PAN Card number using Regular Expression
Write a program to print all permutations of a given string
Recursion
Program for Tower of Hanoi
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Program for Sum of the digits of a given number