# Recursive program to print triangular patterns

We have discussed iterative pattern printing in previous post.

Examples:

```Input : 7
Output :
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 number

Below is the implementation of above approach:

## C++

 `// C++ program to print triangular patterns using Recursive ` `#include ` ` `  `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

 `// Java program to print triangular patterns using Recursive ` `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  ` `        ``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

 `# 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 Code ` `n ``=` `7``; ` ` `  `# Call to printPartten function ` `printPartten(n, ``0``);  ` ` `  `# This code is contributed  ` `# by Akanksha Rai `

## C#

 `// C# program to print triangular ` `// patterns using Recursive ` `using` `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 Code ` `public` `static` `void` `Main()  ` `{  ` `    ``int` `n = 7;  ` ` `  `    ``// Call to printPartten function  ` `    ``printPartten(n, 0);  ` `}  ` `}  ` ` `  `// This code is contributed  ` `// by Subhadeep`

## PHP

 ` `

Output:

```      *
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
```

How to print its reverse pattern?
simply Just put the recursive line end of the function

Example:

```Input : 7
Output :
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
```

## C++

 `// C++ program to print reverse triangular  ` `// patterns using Recursive ` `#include ` ` `  `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 * ` `        ``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

 `// Java program to print reverse triangular  ` `// patterns using Recursive  ` `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  ` `        ``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

# 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 Code n = 7; # Call to printPartten function printPartten(n, 0); # This code is contributed # by Akanksha Rai [tabby title="C#"]

 `// C# program to print reverse triangular  ` `// patterns using Recursive  ` `using` `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 Code ` `public` `static` `void` `Main()  ` `{  ` `    ``int` `n = 7;  ` ` `  `    ``// Call to printPartten function  ` `    ``printPartten(n, 0);  ` `}  ` `}  ` ` `  `// This code is contributed  ` `// by PrinciRaj1992 `

## PHP

 ` `

Output:

```* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
```

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.