Este artículo tiene como objetivo brindar una implementación de Java para la impresión de patrones.
- Patrón de pirámide simple
Java
import java.io.*; // Java code to demonstrate star pattern public class GeeksForGeeks { // Function to demonstrate printing pattern public static void printStars(int n) { int i, j; // outer loop to handle number of rows // n in this case for(i=0; i<n; i++) { // inner loop to handle number spaces // values changing acc. to requirement for(j=2*(n-i); j>=0; j--) { // printing spaces System.out.print(" "); } // inner loop to handle number of columns // values changing acc. to outer loop for(j=0; j<=i; j++) { // printing stars System.out.print("* "); } // ending line after each row System.out.println(); } } // Driver Function public static void main(String args[]) { int n = 5; printStars(n); } }
Producción
* * * * * * * * * * * * * * *
Método: usando el ciclo while
Java
// Java code to demonstrate star pattern //using Recursion import java.util.*; public class GeeksForGeeks { // function to print spaces static void printSpace(int space) { // base case if (space == 0) { return; } System.out.print(" " + " "); // recursively calling printSpace() printSpace(space - 1); } // function to print stars static void printStars(int star) { // base case if (star == 0) { return; } System.out.print("* "); // recursively calling printStars() printStars(star - 1); } // function to print the pattern static void pattern(int n, int num) { // base case if (n == 0) { return; } printSpace(n - 1); printStars(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 Shivesh Kumar Dwivedi
Producción
* * * * * * * * * * * * * * *
Método #2: Usando Recursividad
Java
<div id="highlighter_906579" class="syntaxhighlighter nogutter "><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="comments">//MainFunction</code></div><div class="line number2 index1 alt1"><code class="keyword">public</code> <code class="keyword">class</code> <code class="plain">ReversePattern</code></div><div class="line number3 index2 alt2"><code class="plain">{</code></div><div class="line number4 index3 alt1"><code class="undefined spaces"> </code><code class="keyword">public</code> <code class="keyword">static</code> <code class="keyword">void</code> <code class="plain">main(String[] args) </code></div><div class="line number5 index4 alt2"><code class="undefined spaces"> </code><code class="plain">{</code></div><div class="line number6 index5 alt1"><code class="undefined spaces"> </code><code class="keyword">int</code> <code class="plain">rows = </code><code class="value">7</code><code class="plain">; </code><code class="comments">// Number of Rows we want to print</code></div><div class="line number7 index6 alt2"><code class="undefined spaces"> </code> </div><div class="line number8 index7 alt1"><code class="undefined spaces"> </code> </div><div class="line number9 index8 alt2"><code class="undefined spaces"> </code> </div><div class="line number10 index9 alt1"><code class="undefined spaces"> </code><code class="comments">//Printing the pattern</code></div><div class="line number11 index10 alt2"><code class="undefined spaces"> </code><code class="keyword">for</code> <code class="plain">(</code><code class="keyword">int</code> <code class="plain">i = </code><code class="value">1</code><code class="plain">; i <= rows; i++) </code></div><div class="line number12 index11 alt1"><code class="undefined spaces"> </code><code class="plain">{</code></div><div class="line number13 index12 alt2"><code class="undefined spaces"> </code><code class="keyword">for</code> <code class="plain">(</code><code class="keyword">int</code> <code class="plain">j = </code><code class="value">1</code><code class="plain">; j < i; j++) </code></div><div class="line number14 index13 alt1"><code class="undefined spaces"> </code><code class="plain">{</code></div><div class="line number15 index14 alt2"><code class="undefined spaces"> </code><code class="plain">System.out.print(</code><code class="string">" "</code><code class="plain">);</code></div><div class="line number16 index15 alt1"><code class="undefined spaces"> </code><code class="plain">} </code></div><div class="line number17 index16 alt2"><code class="undefined spaces"> </code><code class="keyword">for</code> <code class="plain">(</code><code class="keyword">int</code> <code class="plain">j = i; j <= rows; j++) </code></div><div class="line number18 index17 alt1"><code class="undefined spaces"> </code><code class="plain">{ </code></div><div class="line number19 index18 alt2"><code class="undefined spaces"> </code><code class="plain">System.out.print(j+</code><code class="string">" "</code><code class="plain">); </code></div><div class="line number20 index19 alt1"><code class="undefined spaces"> </code><code class="plain">} </code></div><div class="line number21 index20 alt2"><code class="undefined spaces"> </code><code class="plain">System.out.println(); </code></div><div class="line number22 index21 alt1"><code class="undefined spaces"> </code><code class="plain">} </code></div><div class="line number23 index22 alt2"><code class="undefined spaces"> </code> </div><div class="line number24 index23 alt1"> </div><div class="line number25 index24 alt2"><code class="undefined spaces"> </code><code class="comments">//Printing the reverse pattern</code></div><div class="line number26 index25 alt1"><code class="undefined spaces"> </code><code class="keyword">for</code> <code class="plain">(</code><code class="keyword">int</code> <code class="plain">i = rows-</code><code class="value">1</code><code class="plain">; i >= </code><code class="value">1</code><code class="plain">; i--) </code></div><div class="line number27 index26 alt2"><code class="undefined spaces"> </code><code class="plain">{</code></div><div class="line number28 index27 alt1"><code class="undefined spaces"> </code><code class="keyword">for</code> <code class="plain">(</code><code class="keyword">int</code> <code class="plain">j = </code><code class="value">1</code><code class="plain">; j < i; j++) </code></div><div class="line number29 index28 alt2"><code class="undefined spaces"> </code><code class="plain">{</code></div><div class="line number30 index29 alt1"><code class="undefined spaces"> </code><code class="plain">System.out.print(</code><code class="string">" "</code><code class="plain">);</code></div><div class="line number31 index30 alt2"><code class="undefined spaces"> </code><code class="plain">} </code></div><div class="line number32 index31 alt1"><code class="undefined spaces"> </code><code class="keyword">for</code> <code class="plain">(</code><code class="keyword">int</code> <code class="plain">j = i; j <= rows; j++)</code></div><div class="line number33 index32 alt2"><code class="undefined spaces"> </code><code class="plain">{</code></div><div class="line number34 index33 alt1"><code class="undefined spaces"> </code><code class="plain">System.out.print(j+</code><code class="string">" "</code><code class="plain">);</code></div><div class="line number35 index34 alt2"><code class="undefined spaces"> </code><code class="plain">} </code></div><div class="line number36 index35 alt1"><code class="undefined spaces"> </code><code class="plain">System.out.println();</code></div><div class="line number37 index36 alt2"><code class="undefined spaces"> </code><code class="plain">}</code></div><div class="line number38 index37 alt1"><code class="undefined spaces"> </code> </div><div class="line number39 index38 alt2"><code class="undefined spaces"> </code><code class="plain">}</code></div><div class="line number40 index39 alt1"><code class="plain">}</code></div></div></td></tr></tbody></table></div>
Producción
* * * * * * * * * * * * * * *
- Después de una rotación de 180 grados
Java
import java.io.*; // Java code to demonstrate number pattern public class GeeksForGeeks { // Function to demonstrate printing pattern public static void printNums(int n) { int i, j,num; // outer loop to handle number of rows // n in this case for(i=0; i<n; i++) { // initialising starting number num=1; // inner loop to handle number of columns // values changing acc. to outer loop for(j=0; j<=i; j++) { // printing num with a space System.out.print(num+ " "); //incrementing value of num num++; } // ending line after each row System.out.println(); } } // Driver Function public static void main(String args[]) { int n = 5; printNums(n); } }
Producción
* * * * * * * * * * * * * * *
Método #2: Usando Recursividad
Java
<div id="highlighter_54798" class="syntaxhighlighter nogutter "><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="comments">// java program to print number pattern using while</code></div><div class="line number2 index1 alt1"><code class="comments">// loop</code></div><div class="line number3 index2 alt2"> </div><div class="line number4 index3 alt1"><code class="keyword">import</code> <code class="plain">java.io.*;</code></div><div class="line number5 index4 alt2"> </div><div class="line number6 index5 alt1"><code class="keyword">class</code> <code class="plain">GFG {</code></div><div class="line number7 index6 alt2"><code class="undefined spaces"> </code><code class="keyword">public</code> <code class="keyword">static</code> <code class="keyword">void</code> <code class="plain">main(String[] args)</code></div><div class="line number8 index7 alt1"><code class="undefined spaces"> </code><code class="plain">{</code></div><div class="line number9 index8 alt2"><code class="undefined spaces"> </code><code class="keyword">int</code> <code class="plain">r = </code><code class="value">1</code><code class="plain">, c = </code><code class="value">1</code><code class="plain">, n = </code><code class="value">5</code><code class="plain">;</code></div><div class="line number10 index9 alt1"><code class="undefined spaces"> </code><code class="comments">// the while loop check the conditions until the</code></div><div class="line number11 index10 alt2"><code class="undefined spaces"> </code><code class="comments">// condition is false. if it is true then enter in</code></div><div class="line number12 index11 alt1"><code class="undefined spaces"> </code><code class="comments">// to loop and execute the statements</code></div><div class="line number13 index12 alt2"><code class="undefined spaces"> </code><code class="keyword">while</code> <code class="plain">(r <= n) {</code></div><div class="line number14 index13 alt1"><code class="undefined spaces"> </code><code class="keyword">while</code> <code class="plain">(c <= r ) {</code></div><div class="line number15 index14 alt2"><code class="undefined spaces"> </code><code class="comments">// printing the required pattern</code></div><div class="line number16 index15 alt1"><code class="undefined spaces"> </code><code class="plain">System.out.print(c+</code><code class="string">" "</code><code class="plain">);</code></div><div class="line number17 index16 alt2"><code class="undefined spaces"> </code><code class="plain">c++;</code></div><div class="line number18 index17 alt1"><code class="undefined spaces"> </code><code class="plain">}</code></div><div class="line number19 index18 alt2"><code class="undefined spaces"> </code><code class="plain">r++;</code></div><div class="line number20 index19 alt1"><code class="undefined spaces"> </code><code class="plain">c = </code><code class="value">1</code><code class="plain">;</code></div><div class="line number21 index20 alt2"><code class="undefined spaces"> </code><code class="comments">// new line after each row</code></div><div class="line number22 index21 alt1"><code class="undefined spaces"> </code><code class="plain">System.out.println();</code></div><div class="line number23 index22 alt2"><code class="undefined spaces"> </code><code class="plain">}</code></div><div class="line number24 index23 alt1"><code class="undefined spaces"> </code><code class="plain">}</code></div><div class="line number25 index24 alt2"><code class="plain">}</code></div></div></td></tr></tbody></table></div>
Producción
* * * * * * * * * * * * * * *
- Triángulo de impresión
Java
import java.io.*; // Java code to demonstrate star pattern public class GeeksForGeeks { // Function to demonstrate printing pattern public static void printNums(int n) { // initialising starting number int i, j, num=1; // outer loop to handle number of rows // n in this case for(i=0; i<n; i++) { // without re assigning num // num = 1; for(j=0; j<=i; j++) { // printing num with a space System.out.print(num+ " "); // incrementing num at each column num = num + 1; } // ending line after each row System.out.println(); } } // Driver Function public static void main(String args[]) { int n = 5; printNums(n); } }
Producción
* * * * * * * * * * * * * * *
Método #2: Usando Recursividad
Java
import java.io.*; // Java code to demonstrate star pattern public class GeeksForGeeks { // Function to demonstrate printing pattern public static void printNums(int n) { // initialising starting number int i, j, num=1; // outer loop to handle number of rows // n in this case for(i=0; i<n; i++) { // without re assigning num // num = 1; for(j=0; j<=i; j++) { // printing num with a space System.out.print(num+ " "); // incrementing num at each column num = num + 1; } // ending line after each row System.out.println(); } } // Driver Function public static void main(String args[]) { int n = 5; printNums(n); } }
Producción
* * * * * * * * * * * * * * *
- Imprimir el reverso de la pirámide
Java
class PrintChristmasTree{ //Value 5 is permanently provided to height variable public static final int height = 5; //Main Function public static void main(String[] args) { //Assigning Width int width = 5; //Assigning Space int space = width*5; int x = 1; //Code to Print Upper Part of the Tree i.e. Pyramids. for(int a = 1;a <= height ;a++){ for(int i = x;i <= width;i++){ for(int j = space;j >= i;j--){ System.out.print(" "); } for(int k = 1;k <= i;k++){ System.out.print("* "); } System.out.println(); } x = x+2; width = width+2; } //Printing Branch of Christmas Tree for(int i = 1;i <= 4;i++){ for(int j = space-3;j >= 1;j--){ System.out.print(" "); } for(int k= 1;k <= 4;k++){ System.out.print("* "); } System.out.println(); } } }
Producción
1 2 3 4 5 6 2 3 4 5 6 3 4 5 6 4 5 6 5 6 6
- Patrón de número con imagen de espejo
Java
Producción
1 2 3 4 5 6 7 2 3 4 5 6 7 3 4 5 6 7 4 5 6 7 5 6 7 6 7 7 6 7 5 6 7 4 5 6 7 3 4 5 6 7 2 3 4 5 6 7 1 2 3 4 5 6 7
- patrón numérico
Java
import java.io.*; // Java code to demonstrate number pattern public class GeeksForGeeks { // Function to demonstrate printing pattern public static void printNums(int n) { int i, j,num; // outer loop to handle number of rows // n in this case for(i=0; i<n; i++) { // initialising starting number num=1; // inner loop to handle number of columns // values changing acc. to outer loop for(j=0; j<=i; j++) { // printing num with a space System.out.print(num+ " "); //incrementing value of num num++; } // ending line after each row System.out.println(); } } // Driver Function public static void main(String args[]) { int n = 5; printNums(n); } }
Producción
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Método 2: usar el ciclo while
Java
// java program to print number pattern using while // loop import java.io.*; class GFG { public static void main(String[] args) { int r = 1, c = 1, n = 5; // the while loop check the conditions until the // condition is false. if it is true then enter in // to loop and execute the statements while (r <= n) { while (c <= r ) { // printing the required pattern System.out.print(c+" "); c++; } r++; c = 1; // new line after each row System.out.println(); } } }
Producción
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
- Números sin reasignar
Java
import java.io.*; // Java code to demonstrate star pattern public class GeeksForGeeks { // Function to demonstrate printing pattern public static void printNums(int n) { // initialising starting number int i, j, num=1; // outer loop to handle number of rows // n in this case for(i=0; i<n; i++) { // without re assigning num // num = 1; for(j=0; j<=i; j++) { // printing num with a space System.out.print(num+ " "); // incrementing num at each column num = num + 1; } // ending line after each row System.out.println(); } } // Driver Function public static void main(String args[]) { int n = 5; printNums(n); } }
Producción
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
- Imprimir árbol de Navidad usando pirámide
Java
class PrintChristmasTree{ //Value 5 is permanently provided to height variable public static final int height = 5; //Main Function public static void main(String[] args) { //Assigning Width int width = 5; //Assigning Space int space = width*5; int x = 1; //Code to Print Upper Part of the Tree i.e. Pyramids. for(int a = 1;a <= height ;a++){ for(int i = x;i <= width;i++){ for(int j = space;j >= i;j--){ System.out.print(" "); } for(int k = 1;k <= i;k++){ System.out.print("* "); } System.out.println(); } x = x+2; width = width+2; } //Printing Branch of Christmas Tree for(int i = 1;i <= 4;i++){ for(int j = space-3;j >= 1;j--){ System.out.print(" "); } for(int k= 1;k <= 4;k++){ System.out.print("* "); } System.out.println(); } } }
Producción
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Este artículo es una contribución de Nikhil Meherwal (S. Shafaq) . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
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