Dado un número n, imprima un patrón triangular. Se nos permite usar solo un bucle.
Ejemplo:
Input: 7 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Usamos un bucle for simple y en el bucle mantenemos dos variables para el recuento de líneas y el recuento de estrellas actual. Si el número actual de estrellas es menor que el número actual de líneas, imprimimos una estrella y continuamos. De lo contrario, imprimimos una nueva línea e incrementamos el recuento de líneas.
C
// C++ program to print a pattern using single // loop and continue statement #include<bits/stdc++.h> using namespace std; // printPattern function to print pattern void printPattern(int n) { // Variable initialization int line_no = 1; // Line count // Loop to print desired pattern int curr_star = 0; for (int line_no = 1; line_no <= n; ) { // If current star count is less than // current line number if (curr_star < line_no) { cout << "* "; curr_star++; continue; } // Else time to print a new line if (curr_star == line_no) { cout << "\n"; line_no++; curr_star = 0; } } } // Driver code int main() { printPattern(7); return 0; }
Java
// Java program to print a pattern using single // loop and continue statement import java.io.*; class GFG { // printPattern function to print pattern static void printPattern(int n) { // Variable initialization // Line count int line_no = 1; // Loop to print desired pattern int curr_star = 0; for ( line_no = 1; line_no <= n;) { // If current star count is less than // current line number if (curr_star < line_no) { System.out.print ( "* "); curr_star++; continue; } // Else time to print a new line if (curr_star == line_no) { System.out.println (""); line_no++; curr_star = 0; } } } // Driver code public static void main (String[] args) { printPattern(7); } } // This code is contributed by vt_m
Python 3
# Python 3 program to print # a pattern using single # loop and continue statement # printPattern function # to print pattern def printPattern(n): # Variable initialization line_no = 1 # Line count # Loop to print # desired pattern curr_star = 0 line_no = 1 while(line_no <= n ): # If current star count # is less than current # line number if (curr_star < line_no): print("* ", end = "") curr_star += 1 continue # Else time to print # a new line if (curr_star == line_no): print("") line_no += 1 curr_star = 0 # Driver code printPattern(7) # This code is contributed # by Smitha
C#
// C# program to print a pattern using single // loop and continue statement using System; class GFG { // printPattern function to print pattern static void printPattern(int n) { // Variable initialization // Line count int line_no = 1; // Loop to print desired pattern int curr_star = 0; for ( line_no = 1; line_no <= n;) { // If current star count is less than // current line number if (curr_star < line_no) { Console.Write ( "* "); curr_star++; continue; } // Else time to print a new line if (curr_star == line_no) { Console.WriteLine (); line_no++; curr_star = 0; } } } // Driver code public static void Main () { printPattern(7); } } // This code is contributed by vt_m
PHP
<?php // php program to print a // pattern using single loop // and continue statement // printPattern function // to print pattern function printPattern($n) { // Variable initialization $line_no = 1; // Line count // Loop to print desired pattern $curr_star = 0; for ($line_no = 1; $line_no <= $n😉 { // If current star count is less // than current line number if ($curr_star < $line_no) { echo "* "; $curr_star++; continue; } // Else time to print // a new line if ($curr_star == $line_no) { echo "\n"; $line_no++; $curr_star = 0; } } } // Driver code $n=7; printPattern($n); // This code is contributed by mits ?>
Javascript
<script> // JavaScript program to print a pattern using single // loop and continue statement // printPattern function to print pattern function printPattern(n) { // Variable initialization var line_no = 1; // Line count // Loop to print desired pattern var curr_star = 0; for (var line_no = 1; line_no <= n; ) { // If current star count is less than // current line number if (curr_star < line_no) { document.write("* "); curr_star++; continue; } // Else time to print a new line if (curr_star == line_no) { document.write("<br>"); line_no++; curr_star = 0; } } } // Driver code printPattern(7); // This code is contributed by rdtank. </script>
Producción:
* * * * * * * * * * * * * * * * * * * * * * * * * * * *
Complejidad de tiempo: O(n)
Espacio auxiliar: O (1)
Consulte la publicación a continuación para obtener un enfoque más.
Imprime el patrón usando solo un lazo
Este artículo es una contribución de Ashish Varshney . Si le gusta GeeksforGeeks y le gustaría contribuir, también puede escribir un artículo usando contribuya.geeksforgeeks.org o envíe su artículo por correo a contribuya@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