Hemos discutido la impresión de patrones iterativos en una publicación anterior .
Ejemplos:
Input : 7 Output : * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Algoritmo:-
paso 1:- primero piense en la condición base, es decir, número menor que 0
paso 2:-haga las llamadas recursivas hasta el número menor que 0, es decir:- printPartten(n-1, k+1);
paso 3: imprima los espacios
paso 4: luego imprima * hasta el número
A continuación se muestra la implementación del enfoque anterior:
C++
// 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
// 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); } }
Python3
# 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
<?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 function printPartten($n, 0); // This code is contributed by jit_t ?>
Javascript
<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>
Producción:
* * * * * * * * * * * * * * * * * * * * * * * * * * * *
¿Cómo imprimir su patrón inverso?
Simplemente coloque el final de la línea recursiva de la función
. Ejemplo:
Input : 7 Output : * * * * * * * * * * * * * * * * * * * * * * * * * * * *
C++
// 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
// 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
// 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
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
<?php // PHP program to print reverse triangular // patterns using Recursive function 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 function printPartten($n, 0); // This code is contributed // by Mukul singh ?>
Javascript
<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>
Producción:
* * * * * * * * * * * * * * * * * * * * * * * * * * * *
Complejidad temporal: O(n 2 )
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA