# Programs for Printing Pyramid Patterns using Recursion

Last Updated : 13 Jan, 2023

• Simple triangle pattern:

## C++

 `// C++ code to demonstrate star pattern` `#include ` `using` `namespace` `std;`   `// function to print a row` `void` `printn(``int` `num)` `{` `    ``// base case` `    ``if` `(num == 0)` `        ``return``;` `    ``cout << ``"* "``;`   `    ``// recursively calling printn()` `    ``printn(num - 1);` `}`   `// function to print the pattern` `void` `pattern(``int` `n, ``int` `i)` `{` `    ``// base case` `    ``if` `(n == 0)` `        ``return``;` `    ``printn(i);` `    ``cout << endl;`   `    ``// recursively calling pattern()` `    ``pattern(n - 1, i + 1);` `}`   `// driver function` `int` `main()` `{` `    ``int` `n = 5;` `    ``pattern(n, 1);` `    ``return` `0;` `}`

## Java

 `// Java code to demonstrate star pattern` `import` `java.io.*;`   `class` `GFG ` `{` `    `  `// function to print a row` `static` `void` `printn(``int` `num)` `{` `    ``// base case` `    ``if` `(num == ``0``)` `        ``return``;` `    ``System.out.print (``"* "``);`   `    ``// recursively calling printn()` `    ``printn(num - ``1``);` `}`   `// function to print the pattern` `static` `void` `pattern(``int` `n, ``int` `i)` `{` `    ``// base case` `    ``if` `(n == ``0``)` `        ``return``;` `    ``printn(i);` `    ``System.out.println();`   `    ``// recursively calling pattern()` `    ``pattern(n - ``1``, i + ``1``);` `}`   `// Driver code` `public` `static` `void` `main (String[] args) ` `{`   `    ``int` `n = ``5``;` `    ``pattern(n, ``1``);` `}` `}`   `// This code is contributed by ajit.`

## Python3

 `# Python3 code to demonstrate star pattern`   `# function to print a row` `def` `printn(num):` `    `  `    ``# base case` `    ``if` `(num ``=``=` `0``):` `        ``return` `    ``print``(``"*"``, end ``=` `" "``)`   `    ``# recursively calling printn()` `    ``printn(num ``-` `1``)`   `# function to print the pattern` `def` `pattern(n, i):` `    `  `    ``# base case` `    ``if` `(n ``=``=` `0``):` `        ``return` `    ``printn(i)` `    ``print``(``"\n"``, end ``=` `"")` `    `  `    ``# recursively calling pattern()` `    ``pattern(n ``-` `1``, i ``+` `1``)`   `# Driver Code` `if` `__name__ ``=``=` `'__main__'``:` `    ``n ``=` `5` `    ``pattern(n, ``1``)`   `# This code is contributed by` `# Surendra_Gangwar`

## C#

 `// C# code to demonstrate star pattern` `using` `System;`   `class` `GFG` `{` `        `  `// function to print a row` `static` `void` `printn(``int` `num)` `{` `    ``// base case` `    ``if` `(num == 0)` `        ``return``;` `    ``Console.Write(``"* "``);`   `    ``// recursively calling printn()` `    ``printn(num - 1);` `}`   `// function to print the pattern` `static` `void` `pattern(``int` `n, ``int` `i)` `{` `    ``// base case` `    ``if` `(n == 0)` `        ``return``;` `    ``printn(i);` `    ``Console.WriteLine();`   `    ``// recursively calling pattern()` `    ``pattern(n - 1, i + 1);` `}`   `// Driver code` `static` `public` `void` `Main ()` `{` `    ``int` `n = 5;` `    ``pattern(n, 1);` `}` `}`   `// This code is contributed by akt_mit.`

## PHP

 ``

## Javascript

 ``

Output:

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

• After 180-degree rotation:

## C++

 `// C++ code to demonstrate star pattern`   `#include ` `using` `namespace` `std;`   `// function to print spaces` `void` `print_space(``int` `space)` `{` `    ``// base case` `    ``if` `(space == 0)` `        ``return``;` `    ``cout << ``" "` `         ``<< ``" "``;`   `    ``// recursively calling print_space()` `    ``print_space(space - 1);` `}`   `// function to print asterisks` `void` `print_asterisk(``int` `asterisk)` `{` `    ``// base case` `    ``if` `(asterisk == 0)` `        ``return``;` `    ``cout << ``"* "``;`   `    ``// recursively calling print_asterisk()` `    ``print_asterisk(asterisk - 1);` `}`   `// function to print the pattern` `void` `pattern(``int` `n, ``int` `num)` `{` `    ``// base case` `    ``if` `(n == 0)` `        ``return``;` `    ``print_space(n - 1);` `    ``print_asterisk(num - n + 1);` `    ``cout << endl;`   `    ``// recursively calling pattern()` `    ``pattern(n - 1, num);` `}`   `// driver function` `int` `main()` `{` `    ``int` `n = 5;` `    ``pattern(n, n);` `    ``return` `0;` `}`

## Java

 `// Java code to demonstrate star pattern ` `import` `java.util.*;`   `class` `GFG` `{`   `    ``// function to print spaces ` `    ``static` `void` `print_space(``int` `space)` `    ``{` `        ``// base case ` `        ``if` `(space == ``0``) ` `        ``{` `            ``return``;` `        ``}` `        ``System.out.print(``" "` `+ ``" "``);`   `        ``// recursively calling print_space() ` `        ``print_space(space - ``1``);` `    ``}`   `    ``// function to print asterisks ` `    ``static` `void` `print_asterisk(``int` `asterisk)` `    ``{` `        ``// base case ` `        ``if` `(asterisk == ``0``)` `        ``{` `            ``return``;` `        ``}` `        ``System.out.print(``"* "``);`   `        ``// recursively calling print_asterisk() ` `        ``print_asterisk(asterisk - ``1``);` `    ``}`   `    ``// function to print the pattern ` `    ``static` `void` `pattern(``int` `n, ``int` `num)` `    ``{` `        ``// base case ` `        ``if` `(n == ``0``)` `        ``{` `            ``return``;` `        ``}` `        ``print_space(n - ``1``);` `        ``print_asterisk(num - n + ``1``);` `        ``System.out.println();`   `        ``// recursively calling pattern() ` `        ``pattern(n - ``1``, num);` `    ``}`   `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{` `        ``int` `n = ``5``;` `        ``pattern(n, n);` `    ``}` `} `   `// This code is contributed by Rajput-Ji`

## Python3

 `# Python3 code to demonstrate star pattern `   `# function to print spaces` `def` `print_space(space):` `    `  `    ``# base case` `    ``if` `(space ``=``=` `0``):` `        ``return``;` `    `  `    ``print``(``" "``, end``=``" "``);`   `    ``# recursively calling print_space()` `    ``print_space(space ``-` `1``);`   `# function to print asterisks` `def` `print_asterisk(asterisk):` `    `  `    ``# base case` `    ``if` `(asterisk ``=``=` `0``):` `        ``return``;` `    `  `    ``print``(``"*"``, end ``=``" "``);`   `    ``# recursively calling print_asterisk()` `    ``print_asterisk(asterisk ``-` `1``);`   `# function to print the pattern` `def` `pattern(n, num):` `    `  `    ``# base case` `    ``if` `(n ``=``=` `0``):` `        ``return``;` `    `  `    ``print_space(n ``-` `1``);` `    ``print_asterisk(num ``-` `n ``+` `1``);` `    ``print``();`   `    ``# recursively calling pattern()` `    ``pattern(n ``-` `1``, num);`   `# Driver code` `if` `__name__ ``=``=` `'__main__'``:` `    ``n ``=` `5``;` `    ``pattern(n, n);` `    `  `# This code is contributed by 29AjayKumar`

## C#

 `// C# code to demonstrate star pattern` `using` `System;`   `class` `GFG` `{` `    `  `// function to print spaces` `static` `void` `print_space(``int` `space)` `{` `    ``// base case` `    ``if` `(space == 0)` `        ``return``;` `    ``Console.Write(``" "` `+ ``" "``);`   `    ``// recursively calling print_space()` `    ``print_space(space - 1);` `}`   `// function to print asterisks` `static` `void` `print_asterisk(``int` `asterisk)` `{` `    ``// base case` `    ``if` `(asterisk == 0)` `        ``return``;` `    ``Console.Write(``"* "``);`   `    ``// recursively calling print_asterisk()` `    ``print_asterisk(asterisk - 1);` `}`   `// function to print the pattern` `static` `void` `pattern(``int` `n, ``int` `num)` `{` `    ``// base case` `    ``if` `(n == 0)` `        ``return``;` `    ``print_space(n - 1);` `    ``print_asterisk(num - n + 1);` `    ``Console.WriteLine();`   `    ``// recursively calling pattern()` `    ``pattern(n - 1, num);` `}`   `// Driver code` `public` `static` `void` `Main()` `{` `    ``int` `n = 5;` `    ``pattern(n, n);` `}` `}`   `// This code is contributed by Akanksha Rai`

## PHP

 ``

## Javascript

 ``

Output:

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

• Printing Pyramid:

## C++

 `// C++ code to demonstrate star pattern`   `#include ` `using` `namespace` `std;`   `// function to print spaces` `void` `print_space(``int` `space)` `{` `    ``// base case` `    ``if` `(space == 0)` `        ``return``;` `    ``cout << ``" "``;`   `    ``// recursively calling print_space()` `    ``print_space(space - 1);` `}`   `// function to print asterisks` `void` `print_asterisk(``int` `asterisk)` `{` `    ``// base case` `    ``if` `(asterisk == 0)` `        ``return``;` `    ``cout << ``"* "``;`   `    ``// recursively calling asterisk()` `    ``print_asterisk(asterisk - 1);` `}`   `// function to print the pattern` `void` `pattern(``int` `n, ``int` `num)` `{` `    ``// base case` `    ``if` `(n == 0)` `        ``return``;` `    ``print_space(n - 1);` `    ``print_asterisk(num - n + 1);` `    ``cout << endl;`   `    ``// recursively calling pattern()` `    ``pattern(n - 1, num);` `}`   `// driver function` `int` `main()` `{` `    ``int` `n = 5;` `    ``pattern(n, n);` `    ``return` `0;` `}`

## Java

 `// Java code to demonstrate star pattern` `import` `java.util.*;`   `class` `GFG` `{`   `// function to print spaces` `static` `void` `print_space(``int` `space)` `{` `    ``// base case` `    ``if` `(space == ``0``)` `        ``return``;` `    ``System.out.print(``" "``);`   `    ``// recursively calling print_space()` `    ``print_space(space - ``1``);` `}`   `// function to print asterisks` `static` `void` `print_asterisk(``int` `asterisk)` `{` `    ``// base case` `    ``if` `(asterisk == ``0``)` `        ``return``;` `    ``System.out.print(``"* "``);`   `    ``// recursively calling asterisk()` `    ``print_asterisk(asterisk - ``1``);` `}`   `// function to print the pattern` `static` `void` `pattern(``int` `n, ``int` `num)` `{` `    ``// base case` `    ``if` `(n == ``0``)` `        ``return``;` `    ``print_space(n - ``1``);` `    ``print_asterisk(num - n + ``1``);` `    ``System.out.println(``""``);`   `    ``// recursively calling pattern()` `    ``pattern(n - ``1``, num);` `}`   `// Driver code` `public` `static` `void` `main(String[] args)` `{` `    ``int` `n = ``5``;` `    ``pattern(n, n);` `}` `}`   `// This code is contributed by 29AjayKumar`

## Python3

 `# Python3 code to demonstrate star pattern`   `# function to print spaces` `def` `print_space(space):` `    `  `    ``# base case` `    ``if` `(space ``=``=` `0``):` `        ``return``;` `    ``print``(``" "``, end ``=` `"");`   `    ``# recursively calling print_space()` `    ``print_space(space ``-` `1``);`   `# function to print asterisks` `def` `print_asterisk(asterisk):` `    `  `    ``# base case` `    ``if``(asterisk ``=``=` `0``):` `        ``return``;` `    ``print``(``"* "``, end ``=` `"");`   `    ``# recursively calling asterisk()` `    ``print_asterisk(asterisk ``-` `1``);`   `# function to print the pattern` `def` `pattern(n, num):` `    `  `    ``# base case` `    ``if` `(n ``=``=` `0``):` `        ``return``;` `    ``print_space(n ``-` `1``);` `    ``print_asterisk(num ``-` `n ``+` `1``);` `    ``print``("");`   `    ``# recursively calling pattern()` `    ``pattern(n ``-` `1``, num);`   `# Driver Code` `n ``=` `5``;` `pattern(n, n);`   `# This code contributed by PrinciRaj1992 `

## C#

 `// C# code to demonstrate star pattern` `using` `System;`   `class` `GFG` `{`   `// function to print spaces` `static` `void` `print_space(``int` `space)` `{` `    ``// base case` `    ``if` `(space == 0)` `        ``return``;` `    ``Console.Write(``" "``);`   `    ``// recursively calling print_space()` `    ``print_space(space - 1);` `}`   `// function to print asterisks` `static` `void` `print_asterisk(``int` `asterisk)` `{` `    ``// base case` `    ``if` `(asterisk == 0)` `        ``return``;` `    ``Console.Write(``"* "``);`   `    ``// recursively calling asterisk()` `    ``print_asterisk(asterisk - 1);` `}`   `// function to print the pattern` `static` `void` `pattern(``int` `n, ``int` `num)` `{` `    ``// base case` `    ``if` `(n == 0)` `        ``return``;` `    ``print_space(n - 1);` `    ``print_asterisk(num - n + 1);` `    ``Console.WriteLine(``""``);`   `    ``// recursively calling pattern()` `    ``pattern(n - 1, num);` `}`   `// Driver code` `public` `static` `void` `Main(String[] args)` `{` `    ``int` `n = 5;` `    ``pattern(n, n);` `}` `}`   `// This code is contributed by 29AjayKumar `

## PHP

 ``

## Javascript

 ``

Output:

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

• Number Pattern

## C++

 `// C++ code to demonstrate printing pattern of numbers`   `#include ` `using` `namespace` `std;`   `// function to print a row` `void` `print_row(``int` `no, ``int` `val)` `{` `    ``// base case` `    ``if` `(no == 0)` `        ``return``;` `    ``cout << val << ``" "``;`   `    ``// recursively calling print_row()` `    ``print_row(no - 1, val);` `}`   `// function to print the pattern` `void` `pattern(``int` `n, ``int` `num)` `{` `    ``// base case` `    ``if` `(n == 0)` `        ``return``;` `    ``print_row(num - n + 1, num - n + 1);` `    ``cout << endl;`   `    ``// recursively calling pattern()` `    ``pattern(n - 1, num);` `}` `int` `main()` `{` `    ``int` `n = 5;` `    ``pattern(n, n);` `}`

## Java

 `// Java code to demonstrate printing ` `// pattern of numbers` `class` `GFG` `{` `    `  `// function to print a row` `static` `void` `print_row(``int` `no, ``int` `val)` `{` `    ``// base case` `    ``if` `(no == ``0``)` `        ``return``;` `    ``System.out.print(val + ``" "``);`   `    ``// recursively calling print_row()` `    ``print_row(no - ``1``, val);` `}`   `// function to print the pattern` `static` `void` `pattern(``int` `n, ``int` `num)` `{` `    ``// base case` `    ``if` `(n == ``0``)` `        ``return``;` `    ``print_row(num - n + ``1``, num - n + ``1``);` `    ``System.out.println();`   `    ``// recursively calling pattern()` `    ``pattern(n - ``1``, num);` `}`   `// Driver Code` `public` `static` `void` `main(String[] args)` `{` `    ``int` `n = ``5``;` `    ``pattern(n, n);` `}` `}`   `// This code is contributed by Code_Mech.`

## Python3

 `# Python3 code to demonstrate printing` `# pattern of numbers`   `# function to print a row` `def` `print_row(no, val):` `    `  `    ``# base case` `    ``if` `(no ``=``=` `0``):` `        ``return``;` `    ``print``(val , end``=``" "``);`   `    ``# recursively calling print_row()` `    ``print_row(no ``-` `1``, val);`   `# function to print the pattern` `def` `pattern(n, num):` `    ``# base case` `    ``if` `(n ``=``=` `0``):` `        ``return``;` `    ``print_row(num ``-` `n ``+` `1``, num ``-` `n ``+` `1``);` `    ``print``("");`   `    ``# recursively calling pattern()` `    ``pattern(n ``-` `1``, num);`   `# Driver Code` `n ``=` `5``;` `pattern(n, n);`   `# This code contributed by PrinciRaj1992 `

## C#

 `// C# code to demonstrate printing ` `// pattern of numbers` `using` `System;`   `class` `GFG` `{` `    `  `// function to print a row` `static` `void` `print_row(``int` `no, ``int` `val)` `{` `    ``// base case` `    ``if` `(no == 0)` `        ``return``;` `    ``Console.Write(val + ``" "``);`   `    ``// recursively calling print_row()` `    ``print_row(no - 1, val);` `}`   `// function to print the pattern` `static` `void` `pattern(``int` `n, ``int` `num)` `{` `    ``// base case` `    ``if` `(n == 0)` `        ``return``;` `    ``print_row(num - n + 1, num - n + 1);` `    ``Console.WriteLine();`   `    ``// recursively calling pattern()` `    ``pattern(n - 1, num);` `}`   `// Driver Code` `public` `static` `void` `Main()` `{` `    ``int` `n = 5;` `    ``pattern(n, n);` `}` `}`   `// This code is contributed by Akanksha Rai`

## PHP

 ``

## Javascript

 ``

Output:

```1
2 2
3 3 3
4 4 4 4
5 5 5 5 5```

• Numbers without reassigning:

## C++

 `// C++ code to demonstrate printing pattern of numbers`   `#include ` `using` `namespace` `std;`   `// function to print a row` `int` `print_row(``int` `ct, ``int` `num)` `{` `    ``// base case` `    ``if` `(num == 0)` `        ``return` `ct;` `    ``cout << ct << ``"\t"``;`   `    ``// recursively calling print_row()` `    ``print_row(ct + 1, num - 1);` `}`   `// function to print the pattern` `void` `pattern(``int` `n, ``int` `count, ``int` `num)` `{` `    ``// base case` `    ``if` `(n == 0)` `        ``return``;` `    ``count = print_row(count, num);` `    ``cout << endl;`   `    ``// recursively calling pattern()` `    ``pattern(n - 1, count, num + 1);` `}`   `// driver function` `int` `main()` `{` `    ``int` `n = 5;` `    ``pattern(n, 1, 1);` `}`

## Java

 `// Java code to demonstrate ` `// printing pattern of numbers` `import` `java.io.*;`   `class` `GFG` `{` `    `  `// function to print a row` `static` `int` `print_row(``int` `ct, ``int` `num)` `{` `    ``// base case` `    ``if` `(num == ``0``)` `        ``return` `ct;` `            ``System.out.print(ct + ``"  "``);`   `    ``// recursively calling print_row()` `    ``return` `print_row(ct + ``1``, num - ``1``);` `}`   `// function to print the pattern` `static` `void` `pattern(``int` `n,` `                    ``int` `count, ``int` `num)` `{` `    ``// base case` `    ``if` `(n == ``0``)` `        ``return``;` `    ``count = print_row(count, num);` `            ``System.out.println();`   `    ``// recursively calling pattern()` `    ``pattern(n - ``1``, count, num + ``1``);` `}`   `// Driver Code` `public` `static` `void` `main (String[] args) ` `{` `    ``int` `n = ``5``;` `    ``pattern(n, ``1``, ``1``);` `}` `}`   `// This code is contributed by Tushil`

## Python3

 `# Python3 code to demonstrate ` `# printing pattern of numbers`   `# function to print a row` `def` `print_row(ct, num):` `    `  `    ``# base case` `    ``if` `(num ``=``=` `0``):` `        ``return` `ct;` `    ``print``(ct, end ``=` `" "``);`   `    ``# recursively calling print_row()` `    ``return` `print_row(ct ``+` `1``, num ``-` `1``);`   `# function to print the pattern` `def` `pattern(n, count, num):` `    `  `    ``# base case` `    ``if` `(n ``=``=` `0``):` `        ``return``;` `    ``count ``=` `print_row(count, num);` `    ``print``();`   `    ``# recursively calling pattern()` `    ``pattern(n ``-` `1``, count, num ``+` `1``);`   `# Driver Code` `if` `__name__ ``=``=` `'__main__'``:` `    ``n ``=` `5``;` `    ``pattern(n, ``1``, ``1``);`   `# This code is contributed by 29AjayKumar`

## C#

 `// C# code to demonstrate ` `// printing pattern of numbers` `using` `System;`   `class` `GFG` `{`   `// function to print a row` `static` `int` `print_row(``int` `ct, ``int` `num)` `{` `    ``// base case` `    ``if` `(num == 0)` `        ``return` `ct;` `    ``Console.Write (ct + ``"  "``);`   `    ``// recursively calling print_row()` `    ``return` `print_row(ct + 1, num - 1);` `}`   `// function to print the pattern` `static` `void` `pattern(``int` `n,` `                    ``int` `count, ``int` `num)` `{` `    ``// base case` `    ``if` `(n == 0)` `        ``return``;` `    ``count = print_row(count, num);` `    ``Console.WriteLine();`   `    ``// recursively calling pattern()` `    ``pattern(n - 1, count, num + 1);` `}`   `// Driver Code` `static` `public` `void` `Main ()` `{` `    ``int` `n = 5;` `    ``pattern(n, 1, 1);` `}` `}`   `// This code is contributed by ajit`

## Javascript

 ``

Output:

```1
2    3
4    5    6
7    8    9    10
11    12    13    14    15```

• Character Pattern:

## C++

 `// C++ code to demonstrate printing pattern of alphabets`   `#include ` `using` `namespace` `std;`   `// function to print a row` `void` `print_row(``int` `no, ``int` `val)` `{` `    ``// base case` `    ``if` `(no == 0)` `        ``return``;` `    ``cout << (``char``)(val + 64) << ``" "``;`   `    ``// recursively calling print_row()` `    ``print_row(no - 1, val);` `}`   `// function to print the pattern` `void` `pattern(``int` `n, ``int` `num)` `{` `    ``// base case` `    ``if` `(n == 0)` `        ``return``;` `    ``print_row(num - n + 1, num - n + 1);` `    ``cout << endl;`   `    ``// recursively calling pattern()` `    ``pattern(n - 1, num);` `}` `int` `main()` `{` `    ``int` `n = 5;` `    ``pattern(n, n);` `}`

## Java

 `// Java code to demonstrate printing pattern of alphabets` `class` `GFG` `{`   `// function to print a row` `static` `void` `print_row(``int` `no, ``int` `val)` `{` `    ``// base case` `    ``if` `(no == ``0``)` `        ``return``;` `    ``System.out.print((``char``)(val + ``64``) + ``" "``);`   `    ``// recursively calling print_row()` `    ``print_row(no - ``1``, val);` `}`   `// function to print the pattern` `static` `void` `pattern(``int` `n, ``int` `num)` `{` `    ``// base case` `    ``if` `(n == ``0``)` `        ``return``;` `    ``print_row(num - n + ``1``, num - n + ``1``);` `    ``System.out.println();`   `    ``// recursively calling pattern()` `    ``pattern(n - ``1``, num);` `}`   `// Driver code` `public` `static` `void` `main(String[] args)` `{` `    ``int` `n = ``5``;` `    ``pattern(n, n);` `}` `}`   `// This code is contributed by Rajput-Ji`

## Python3

 `# Python3 code to demonstrate printing pattern of alphabets`     `# function to print a row` `def` `print_row(no, val):` `    ``# base case` `    ``if` `(no ``=``=` `0``):` `        ``return``;` `    ``print``(``chr``(val ``+` `64``), end``=``" "``);`   `    ``# recursively calling print_row()` `    ``print_row(no ``-` `1``, val);`     `# function to print the pattern` `def` `pattern(n, num):` `    ``# base case` `    ``if` `(n ``=``=` `0``):` `        ``return``;` `    ``print_row(num ``-` `n ``+` `1``, num ``-` `n ``+` `1``);` `    ``print``();`   `    ``# recursively calling pattern()` `    ``pattern(n ``-` `1``, num);`     `# Driver code` `if` `__name__ ``=``=` `'__main__'``:` `    ``n ``=` `5``;` `    ``pattern(n, n);`   `# This code is contributed by 29AjayKumar`

## C#

 `// C# code to demonstrate printing pattern of alphabets` `using` `System;`   `class` `GFG` `{`   `// function to print a row` `static` `void` `print_row(``int` `no, ``int` `val)` `{` `    ``// base case` `    ``if` `(no == 0)` `        ``return``;` `    ``Console.Write((``char``)(val + 64) + ``" "``);`   `    ``// recursively calling print_row()` `    ``print_row(no - 1, val);` `}`   `// function to print the pattern` `static` `void` `pattern(``int` `n, ``int` `num)` `{` `    ``// base case` `    ``if` `(n == 0)` `        ``return``;` `    ``print_row(num - n + 1, num - n + 1);` `    ``Console.WriteLine();`   `    ``// recursively calling pattern()` `    ``pattern(n - 1, num);` `}`   `// Driver code` `public` `static` `void` `Main(String[] args)` `{` `    ``int` `n = 5;` `    ``pattern(n, n);` `}` `}`   `// This code is contributed by 29AjayKumar`

## Javascript

 ``

Output:

```A
B B
C C C
D D D D
E E E E E```

• Continuous Character pattern:

## C++

 `#include ` `using` `namespace` `std;`   `// function to print a row` `int` `print_row(``int` `ct, ``int` `num)` `{` `    ``// base case` `    ``if` `(num == 0)` `        ``return` `ct;` `    ``cout << (``char``)(ct + 64) << ``" "``;`   `    ``// recursively calling print_row()` `    ``print_row(ct + 1, num - 1);` `}`   `// function to print the pattern` `void` `pattern(``int` `n, ``int` `count, ``int` `num)` `{` `    ``// base case` `    ``if` `(n == 0)` `        ``return``;` `    ``count = print_row(count, num);` `    ``cout << endl;`   `    ``// recursively calling pattern()` `    ``pattern(n - 1, count, num + 1);` `}`   `// driver function` `int` `main()` `{` `    ``int` `n = 5;` `    ``pattern(n, 1, 1);` `}`

## Java

 `class` `GFG` `{`   `// function to print a row` `static` `int` `print_row(``int` `ct, ``int` `num)` `{` `    ``// base case` `    ``if` `(num == ``0``)` `        ``return` `ct;` `    ``System.out.print((``char``)(ct + ``64``) + ``" "``);`   `    ``// recursively calling print_row()` `    ``print_row(ct + ``1``, num - ``1``);` `    ``return` `num + ct;` `    `  `}`   `// function to print the pattern` `static` `void` `pattern(``int` `n, ``int` `count, ``int` `num)` `{` `    ``// base case` `    ``if` `(n == ``0``)` `        ``return``;` `    ``count = print_row(count, num);` `    ``System.out.println();`   `    ``// recursively calling pattern()` `    ``pattern(n - ``1``, count, num + ``1``);` `}`   `// Driver code` `public` `static` `void` `main(String[] args)` `{` `    ``int` `n = ``5``;` `    ``pattern(n, ``1``, ``1``);` `}` `}`   `// This code is contributed by 29AjayKumar`

## Python3

 `# function to print row` `def` `print_row(ct, num):` `    `  `    ``# base case` `    ``if` `(num ``=``=` `0``):` `        ``return` `ct;` `    ``print``(``chr``(ct ``+` `64``), end``=``" "``);`   `    ``# recursively calling print_row()` `    ``print_row(ct ``+` `1``, num ``-` `1``);` `    ``return` `num ``+` `ct;`   `# function to print the pattern` `def` `pattern(n, count, num):`   `    ``# base case` `    ``if` `(n ``=``=` `0``):` `        ``return``;` `    ``count ``=` `print_row(count, num);` `    ``print``();`   `    ``# recursively calling pattern()` `    ``pattern(n ``-` `1``, count, num ``+` `1``);`   `# Driver code` `if` `__name__ ``=``=` `'__main__'``:` `    ``n ``=` `5``;` `    ``pattern(n, ``1``, ``1``);`   `# This code is contributed by Rajput-Ji`

## C#

 `using` `System;`   `class` `GFG` `{`   `// function to print a row` `static` `int` `print_row(``int` `ct, ``int` `num)` `{` `    ``// base case` `    ``if` `(num == 0)` `        ``return` `ct;` `    ``Console.Write((``char``)(ct + 64) + ``" "``);`   `    ``// recursively calling print_row()` `    ``print_row(ct + 1, num - 1);` `    ``return` `num + ct;`   `}`   `// function to print the pattern` `static` `void` `pattern(``int` `n, ``int` `count, ``int` `num)` `{` `    ``// base case` `    ``if` `(n == 0)` `        ``return``;` `    ``count = print_row(count, num);` `    ``Console.WriteLine();`   `    ``// recursively calling pattern()` `    ``pattern(n - 1, count, num + 1);` `}`   `// Driver code` `public` `static` `void` `Main(String[] args)` `{` `    ``int` `n = 5;` `    ``pattern(n, 1, 1);` `}` `}`   `// This code is contributed by 29AjayKumar`

## Javascript

 ``

Output:

```A
B C
D E F
G H I J
K L M N O```

### Time Complexity and Space Complexity of the above Programs

Time Complexity: O(n2)
Auxiliary Space: O(n2), due to recursion stack.

Previous
Next