Recursive program to print triangular patterns - GeeksforGeeks

08 Jun, 2021
We have discussed iterative pattern printing in previous post. Examples:  
Input : 7
Output :                    
      *
     * *
    * * *
   * * * * 
  * * * * *
 * * * * * *
* * * * * * *
 
Algorithm:- step 1:- first think for the base condition i.e. number less then 0 step 2:-do the recursive calls till number less then 0 i.e:- printPartten(n-1, k+1); step 3:-print the spaces step 4:-then print * till numberBelow is the implementation of above approach: 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to print triangular patterns using Recursive#include <iostream> using namespace std;void printPartten(int n, int k){    if (n < 0) // Base condition        return;     // Recursive call    printPartten(n - 1, k + 1);     int i;    for (i = 0; i < k; i++) // it makes spaces        cout << " ";    for (i = 0; i < n; i++) // for print *        printf("* ");    printf("\n"); // for next line} int main(){    int n = 7;     // Call to printPartten function    printPartten(n, 0);    return 0;}
// Java program to print triangular patterns using Recursiveclass GFG{static void printPartten(int n, int k){    if (n < 0) // Base condition        return;     // Recursive call    printPartten(n - 1, k + 1);     int i;    for (i = 0; i < k; i++) // it makes spaces        System.out.printf(" ");    for (i = 0; i < n; i++) // for print *        System.out.printf("* ");    System.out.printf("\n"); // for next line} public static void main(String[]args){    int n = 7;     // Call to printPartten function    printPartten(n, 0);}}
# Python 3 program to print triangular# patterns using Recursive def printPartten(n, k):         if (n < 0): # Base condition        return;     # Recursive call    printPartten(n - 1, k + 1);     for i in range(0, k): # it makes spaces        print(" ", end="");    for i in range(0, n): # for print *        print("* ", end = "");    print("\n", end=""); # for next line # Driver Coden = 7; # Call to printPartten functionprintPartten(n, 0); # This code is contributed# by Akanksha Rai
// C# program to print triangular// patterns using Recursiveusing System; class GFG{static void printPartten(int n, int k){    if (n < 0) // Base condition        return;     // Recursive call    printPartten(n - 1, k + 1);     int i;    for (i = 0; i < k; i++) // it makes spaces        Console.Write(" ");    for (i = 0; i < n; i++) // for print *        Console.Write("* ");    Console.Write("\n"); // for next line} // Driver Codepublic static void Main(){    int n = 7;     // Call to printPartten function    printPartten(n, 0);}} // This code is contributed// by Subhadeep
<?php// PHP program to print triangular// patterns using Recursive function printPartten($n, $k){    if ($n < 0) // Base condition        return;     // Recursive call    printPartten($n - 1, $k + 1);     for ($i = 0; $i < $k; $i++) // it makes spaces        echo " ";    for ($i = 0; $i < $n; $i++) // for print *        echo ("* ");    echo ("\n"); // for next line} // Driver Code$n = 7; // Call to printPartten functionprintPartten($n, 0); // This code is contributed by jit_t?>
<script>// javascript program to print triangular patterns using Recursive     function printPartten(n , k) {        if (n < 0) // Base condition            return;         // Recursive call        printPartten(n - 1, k + 1);         var i;        for (i = 0; i < k; i++) // it makes spaces             document.write(" ");        for (i = 0; i < n; i++) // for print *             document.write("* ");         document.write("<br/>"); // for next line    }              var n = 7;         // Call to printPartten function        printPartten(n, 0); // This code is contributed by Rajput-Ji</script>
      * 
     * * 
    * * * 
   * * * * 
  * * * * * 
 * * * * * * 
* * * * * * *
 
How to print its reverse pattern? simply Just put the recursive line end of the function Example:  
Input : 7
Output : 
* * * * * * *
 * * * * * * 
  * * * * *    
   * * * *  
    * * *    
     * *          
      *
 
C++
C
Java
Python 3
C#
PHP
Javascript
// C++ program to print reverse triangular// patterns using Recursive#include <bits/stdc++.h>using namespace std; void printPartten(int n, int k){    if (n < 0) // Base condition        return;    int i;    for (i = 0; i < k; i++) // it makes spaces        cout <<" ";    for (i = 0; i < n; i++) // for print *        cout <<"* ";    cout <<"\n"; // for next line     // Recursive calls    printPartten(n - 1, k + 1);} int main(){    int n = 7;     // Call to printPartten function    printPartten(n, 0);    return 0;}// this code is contributed by shivanisinghss2110
// C program to print reverse triangular// patterns using Recursive#include <stdio.h>#include <stdlib.h> void printPartten(int n, int k){    if (n < 0) // Base condition        return;    int i;    for (i = 0; i < k; i++) // it makes spaces        printf( " ");    for (i = 0; i < n; i++) // for print *        printf("* ");    printf("\n"); // for next line     // Recursive calls    printPartten(n - 1, k + 1);} int main(){    int n = 7;     // Call to printPartten function    printPartten(n, 0);    return 0;}
// Java program to print reverse triangular// patterns using Recursiveclass GFG{static void printPartten(int n, int k){    if (n < 0) // Base condition        return;    int i;    for (i = 0; i < k; i++) // it makes spaces        System.out.print(" ");    for (i = 0; i < n; i++) // for print *        System.out.print("* ");    System.out.print("\n"); // for next line     // Recursive calls    printPartten(n - 1, k + 1);} public static void main(String[] args){    int n = 7;     // Call to printPartten function    printPartten(n, 0);}}
# Python 3 program to print reverse# triangular patterns using Recursive def printPartten(n, k):     if (n < 0): # Base condition        return;    for i in range(0, k): # it makes spaces        print(" ", end = "")    for i in range(0, n): # for print *        print("*", end = " ")    print("\n", end = "") # for next line     # Recursive calls    printPartten(n - 1, k + 1); # Driver Coden = 7; # Call to printPartten functionprintPartten(n, 0); # This code is contributed# by Akanksha Rai
// C# program to print reverse triangular// patterns using Recursiveusing System; class GFG{static void printPartten(int n, int k){    if (n < 0) // Base condition        return;    int i;    for (i = 0; i < k; i++) // it makes spaces        Console.Write(" ");    for (i = 0; i < n; i++) // for print *        Console.Write("* ");    Console.Write("\n"); // for next line     // Recursive calls    printPartten(n - 1, k + 1);} // Driver Codepublic static void Main(){    int n = 7;     // Call to printPartten function    printPartten(n, 0);}} // This code is contributed// by PrinciRaj1992
<?php// PHP program to print reverse triangular// patterns using Recursivefunction printPartten($n, $k){    if ($n < 0) // Base condition        return;     for ($i = 0; $i < $k; $i++) // it makes spaces        echo(" ");    for ($i = 0; $i < $n; $i++) // for print *        echo("* ");    echo("\n"); // for next line     // Recursive calls    printPartten($n - 1, $k + 1);} // Driver Code$n = 7; // Call to printPartten functionprintPartten($n, 0); // This code is contributed// by Mukul singh?>
<script>// C++ program to print reverse triangular// patterns using Recursive function printPartten(n, k){    if (n < 0) // Base condition        return;    let i;    for (i = 0; i < k; i++) // it makes spaces        document.write(" ");    for (i = 0; i < n; i++) // for print *        document.write("* ");    document.write("<br>"); // for next line     // Recursive calls    printPartten(n - 1, k + 1);} //driver code    let n = 7;     // Call to printPartten function    printPartten(n, 0);  // this code is contributed by shivanisinghss2110</script>
* * * * * * * 
 * * * * * * 
  * * * * * 
   * * * * 
    * * * 
     * * 
      *
 
Smitha Dinesh Semwal
tufan_gupta2000
jit_t
princiraj1992
Code_Mech
Akanksha_Rai
Rajput-Ji
Kirti_Mangal
shivanisinghss2110
pattern-printing
triangle
School Programming
pattern-printing
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C++ Classes and Objects
Interfaces in Java
Constructors in C++
Operator Overloading in C++
Copy Constructor in C++
Polymorphism in C++
C++ Data Types
Friend class and function in C++
Introduction To PYTHON
Types of Operating Systems