Dado un número N que denota el número de filas. La tarea es imprimir el patrón en zigzag con N filas como se muestra en los siguientes ejemplos.
Ejemplos:
Input : 2 Output : 1 2 3 Input : 5 Output : 1 2 6 3 7 10 4 8 11 13 5 9 12 14 15
Enfoque:
1. Use un bucle for para imprimir el número de filas.
2. Use dos bucles for para imprimir el espacio y el patrón.
3. Calcule el valor inicial para cada fila con variable t.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ Program to print pyramid // pattern using numbers #include <iostream> using namespace std; // Function to print pyramid pattern class gfg { public : void printPattern(int n) { int t; // outer loop to print rows for (int i = 1; i <= n; i++) { // inner loop to print spaces for (int j = i; j < n; j++) printf("\t"); // calculate initial value t = i; // inner loop to print pattern for (int k = 1; k <= i; k++) { printf("%d\t\t", t); t = t + n - k; } printf("\n"); } } }; // Driver code int main() { gfg g; int n = 6; g.printPattern(n); return 0; } //this code is contributed by Soumik
C
// C Program to print pyramid // pattern using numbers #include <stdio.h> // Function to print pyramid pattern void printPattern(int n) { int t; // outer loop to print rows for (int i = 1; i <= n; i++) { // inner loop to print spaces for (int j = i; j < n; j++) printf("\t"); // calculate initial value t = i; // inner loop to print pattern for (int k = 1; k <= i; k++) { printf("%d\t\t", t); t = t + n - k; } printf("\n"); } } // Driver code int main() { int n = 6; printPattern(n); return 0; }
Java
// Java Program to print pyramid // pattern using numbers class GFG { // Function to print pyramid pattern static void printPattern(int n) { int t; // outer loop to print rows for (int i = 1; i <= n; i++) { // inner loop to print spaces for (int j = i; j < n; j++) System.out.print("\t"); // calculate initial value t = i; // inner loop to print pattern for (int k = 1; k <= i; k++) { System.out.print(t + "\t\t"); t = t + n - k; } System.out.println(); } } // Driver code public static void main(String []args) { int n = 6; printPattern(n); } } // This code is contributed by iAyushRaj
Python3
# Python3 Program to print pyramid # pattern using numbers # Function to print pyramid pattern def printPattern(n): # outer loop to print rows for i in range(1, n + 1): # inner loop to print spaces for j in range(i, n): print("\t", end = "") # calculate initial value t = i # inner loop to print pattern for k in range (1, i + 1): print(t, "\t", "\t", end = "") t = t + n - k print() # Driver code n = 6 printPattern(n) # This code is contributed # by iAyushRaj
C#
// C# Program to print pyramid // pattern using numbers using System; class GFG { // Function to print pyramid pattern static void printPattern(int n) { int t; // outer loop to print rows for (int i = 1; i <= n; i++) { // inner loop to print spaces for (int j = i; j < n; j++) Console.Write("\t"); // calculate initial value t = i; // inner loop to print pattern for (int k = 1; k <= i; k++) { Console.Write(t + "\t\t"); t = t + n - k; } Console.WriteLine(); } } // Driver code public static void Main() { int n = 6; printPattern(n); } } // This code is contributed by iAyushRaj
PHP
<?php // PHP Program to print pyramid // pattern using numbers // Function to print pyramid pattern function printPattern($n) { // outer loop to print rows for ($i = 1; $i <= $n; $i++) { // inner loop to print spaces for ($j = $i; $j < $n; $j++) echo "\t"; // calculate initial value $t = $i; // inner loop to print pattern for ($k = 1; $k <= $i; $k++) { echo "$t \t\t"; $t = $t + $n - $k; } echo "\n"; } } // Driver code $n = 6; printPattern($n); // This code is contributed // by iAyushRaj ?>
Javascript
<script> // JavaScript Program to print pyramid // pattern using numbers // Function to print pyramid pattern function printPattern(n) { var t; // outer loop to print rows for (var i = 1; i <= n; i++) { // inner loop to print spaces for (var j = i; j < n; j++) document.write(" "); // calculate initial value t = i; // inner loop to print pattern for (var k = 1; k <= i; k++) { document.write(t + " "); t = t + n - k; } document.write("<br>"); } } // Driver code var n = 6; printPattern(n); </script>
Producción:
1 2 7 3 8 12 4 9 13 16 5 10 14 17 19 6 11 15 18 20 21
Publicación traducida automáticamente
Artículo escrito por Kasthuri Palanichamy y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA