Este artículo tiene como objetivo dar una implementación recursiva para la impresión de patrones.
- Patrón de triángulo simple:
C++
// C++ code to demonstrate star pattern #include <iostream> 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
<?php // PHP code to demonstrate star pattern // Function to print a row function printn($num) { // base case if ($num == 0) return; echo "* "; // recursively calling printn() printn($num - 1); } // function to print the pattern function pattern($n, $i) { // base case if ($n == 0) return; printn($i); echo "\n"; // recursively calling pattern() pattern($n - 1, $i + 1); } // Driver Code $n = 5; pattern($n, 1); // This code is contributed by @Tushil.. ?>
Javascript
<script> // JavaScript code to demonstrate star pattern // function to print a row function printn(num) { // base case if (num == 0) return; document.write("* "); // recursively calling printn() printn(num - 1); } // function to print the pattern function pattern(n , i) { // base case if (n == 0) return; printn(i); document.write("<br/>"); // recursively calling pattern() pattern(n - 1, i + 1); } // Driver code var n = 5; pattern(n, 1); // This code is contributed by aashish1995 </script>
Producción:
* * * * * * * * * * * * * * *
- Después de una rotación de 180 grados:
C++
// C++ code to demonstrate star pattern #include <iostream> 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 prspaces def print_space(space): # base case if (space == 0): return; print(" ", end=" "); # recursively calling print_space() print_space(space - 1); # function to prasterisks 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
<?php // PHP code to demonstrate star pattern // function to print spaces function print_space($space) { // base case if ($space == 0) return; echo " ", " "; // recursively calling print_space() print_space($space - 1); } // function to print asterisks function print_asterisk($asterisk) { // base case if ($asterisk == 0) return; echo "* "; // recursively calling print_asterisk() print_asterisk($asterisk - 1); } // function to print the pattern function pattern($n, $num) { // base case if ($n == 0) return; print_space($n - 1); print_asterisk(($num - $n) + 1); echo "\n"; // recursively calling pattern() pattern($n - 1, $num); } // Driver Code $n = 5; pattern($n, $n); // This code is contributed by @Tushil. ?>
Javascript
<script> // javascript code to demonstrate star pattern // function to print spaces function print_space(space) { // base case if (space == 0) { return; } document.write(" " + " "); // recursively calling print_space() print_space(space - 1); } // function to print asterisks function print_asterisk(asterisk) { // base case if (asterisk == 0) { return; } document.write("* "); // recursively calling print_asterisk() print_asterisk(asterisk - 1); } // function to print the pattern function pattern(n , num) { // base case if (n == 0) { return; } print_space(n - 1); print_asterisk(num - n + 1); document.write("<br/>"); // recursively calling pattern() pattern(n - 1, num); } // Driver code var n = 5; pattern(n, n); // This code contributed by aashish1995 </script>
Producción:
* * * * * * * * * * * * * * *
- Pirámide de impresión:
C++
// C++ code to demonstrate star pattern #include <iostream> 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
<?php // PHP code to demonstrate star pattern // function to print spaces function print_space($space) { // base case if ($space == 0) return; echo " "; // recursively calling print_space() print_space($space - 1); } // function to print asterisks function print_asterisk($asterisk) { // base case if ($asterisk == 0) return; echo "* "; // recursively calling asterisk() print_asterisk($asterisk - 1); } // function to print the pattern function pattern($n, $num) { // base case if ($n == 0) return; print_space($n - 1); print_asterisk($num - $n + 1); echo "\n"; // recursively calling pattern() pattern($n - 1, $num); } // Driver code $n = 5; pattern($n, $n); // This code is contributed by ajit. ?>
Javascript
<script> // JavaScript code to demonstrate star pattern // function to print spaces function print_space(space) { // base case if (space == 0) return; document.write(" "); // recursively calling print_space() print_space(space - 1); } // function to print asterisks function print_asterisk(asterisk) { // base case if (asterisk == 0) return; document.write("* "); // recursively calling asterisk() print_asterisk(asterisk - 1); } // function to print the pattern function pattern(n,num) { // base case if (n == 0) return; print_space(n - 1); print_asterisk(num - n + 1); document.write("<br>"); // recursively calling pattern() pattern(n - 1, num); } // Driver code let n = 5; pattern(n, n); // This code is contributed by rag2127 </script>
Producción:
* * * * * * * * * * * * * * *
- patrón numérico
C++
// C++ code to demonstrate printing pattern of numbers #include <iostream> 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
<?php // PHP code to demonstrate printing // pattern of numbers // function to print a row function print_row($no, $val) { // base case if ($no == 0) return; echo $val . " "; // recursively calling print_row() print_row($no - 1, $val); } // function to print the pattern function pattern($n, $num) { // base case if ($n == 0) return; print_row($num - $n + 1, $num - $n + 1); echo "\n"; // recursively calling pattern() pattern($n - 1, $num); } // Driver Code $n = 5; pattern($n, $n); // This code is contributed // by Akanksha Rai ?>
Javascript
<script> // JavaScript code to demonstrate // printing pattern of numbers // function to print a row function print_row(no, val) { // base case if (no == 0) return; document.write(val + " "); // recursively calling print_row() print_row(no - 1, val); } // function to print the pattern function pattern(n, num) { // base case if (n == 0) return; print_row(num - n + 1, num - n + 1); document.write("<br>"); // recursively calling pattern() pattern(n - 1, num); } var n = 5; pattern(n, n); </script>
Producción:
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
- Números sin reasignar :
C++
// C++ code to demonstrate printing pattern of numbers #include <iostream> 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 pra 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
<script> // Javascript code to demonstrate // printing pattern of numbers // Function to print a row function print_row(ct, num) { // Base case if (num == 0) return ct; document.write(ct + " "); // Recursively calling print_row() return print_row(ct + 1, num - 1); } // Function to print the pattern function pattern(n, count, num) { // Base case if (n == 0) return; count = print_row(count, num); document.write("<br/>"); // Recursively calling pattern() pattern(n - 1, count, num + 1); } // Driver Code var n = 5; pattern(n, 1, 1); // This code is contributed by aashish1995 </script>
Producción:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
- Patrón de caracteres:
C++
// C++ code to demonstrate printing pattern of alphabets #include <iostream> 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 pra 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
<script> // Javascript code to demonstrate printing pattern of alphabets // function to print a row function print_row(no, val) { // base case if (no == 0) return; document.write(String.fromCharCode(val + 64) + " "); // recursively calling print_row() print_row(no - 1, val); } // function to print the pattern function pattern(n, num) { // base case if (n == 0) return; print_row(num - n + 1, num - n + 1); document.write("</br>"); // recursively calling pattern() pattern(n - 1, num); } let n = 5; pattern(n, n); </script>
Producción:
A B B C C C D D D D E E E E E
- Patrón de carácter continuo:
C++
#include <iostream> 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
<script> // function to print a row function print_row( ct, num) { // base case if (num == 0) return ct; document.write(String.fromCharCode(ct + 64) + " "); // recursively calling print_row() print_row(ct + 1, num - 1); return num + ct; } // function to print the pattern function pattern( n, count, num) { // base case if (n == 0) return; count = print_row(count, num); document.write("<br>"); // recursively calling pattern() pattern(n - 1, count, num + 1); } // Driver code var n = 5; pattern(n, 1, 1); </script>
Producción:
A B C D E F G H I J K L M N O