Patrón triangular inferior hueco

Dado el valor de n, es decir, el número de filas/columnas de un cuadrado, imprima el patrón.
Ejemplos: 
 

Input : n = 8
Output :
 * * * * * * * *
 * * * * * * * *
 *   * * * * * *
 *     * * * * *
 *       * * * *
 *         * * *
 *           * *
 * * * * * * * *

Input : n = 15
Output :
 * * * * * * * * * * * * * * *
 * * * * * * * * * * * * * * *
 *   * * * * * * * * * * * * *
 *     * * * * * * * * * * * *
 *       * * * * * * * * * * *
 *         * * * * * * * * * *
 *           * * * * * * * * *
 *             * * * * * * * *
 *               * * * * * * *
 *                 * * * * * *
 *                   * * * * *
 *                     * * * *
 *                       * * *
 *                         * *
 * * * * * * * * * * * * * * *

A continuación se muestra la implementación para imprimir un patrón triangular inferior hueco: 
 

C++

// C++ implementation of hollow
// lower triangular pattern
#include <bits/stdc++.h>
using namespace std;
 
// function to print the pattern
void print_pattern(int n)
{
    int k = 1;
 
    // for loop to keep track of
    // number of rows
    for (int i = 1; i <= n; i++) {
 
        // for loop to keep track of
        // number of columns
        for (int j = 1; j <= n; j++) {
 
            // if last row or first column
            if (i == n || j == 1)
                cout << " "
                     << "*";
 
            // to skip the lower triangular part
            else if (j < k)
                cout << " "
                     << " ";
 
            // to print star in remaining portion
            else
                cout << " "
                     << "*";
        }
        cout << endl;
        k++;
    }
}
 
// driver code
int main()
{
    int n = 8;
    print_pattern(n);
    return 0;
}

Java

// Java implementation of hollow
// lower triangular pattern
import java.io.*;
 
class GFG {
 
    // function to print the pattern
    static void print_pattern(int n)
    {
         
        int k = 1;
 
        // for loop to keep track of
        // number of rows
        for (int i = 1; i <= n; i++) {
 
            // for loop to keep track of
            // number of columns
            for (int j = 1; j <= n; j++) {
 
                // if last row or first column
                if (i == n || j == 1)
                    System.out.print(" "
                                    + "*");
 
                // to skip the lower triangular
                // part
                else if (j < k)
                    System.out.print(" "
                                    + " ");
 
                // to print star in remaining
                // portion
                else
                    System.out.print(" "
                                    + "*");
            }
            System.out.println();
            k++;
        }
    }
 
    // driver code
    public static void main(String[] args)
    {
        int n = 8;
         
        print_pattern(n);
    }
}
 
// This code is contributed by vt_m

Python3

# Python3 implementation of hollow
# lower triangular pattern
 
# Function to print the pattern
def print_pattern(n):
    k = 1;
 
    # for loop to keep track
    # of number of rows
    for i in range(1, n + 1):
         
        # for loop to keep track
        # of number of columns
        for j in range(1, n + 1):
             
            # if last row or
            # first column
            if (i == n or j == 1):
                print(" *", end = "");
 
            # to skip the lower
            # triangular part
            elif (j < k):
                print(" ", end = " ");
 
            # to print star in
            # remaining portion
            else:
                print(" *", end = "");
        print();
        k += 1;
 
# Driver code
n = 8;
print_pattern(n);
 
# This code is contributed
# by Mithun Kumar

C#

// implementation of hollow
// lower triangular pattern
using System;
 
class GFG {
 
    // function to print the pattern
    static void print_pattern(int n)
    {
 
        int k = 1;
 
        // for loop to keep track of
        // number of rows
        for (int i = 1; i <= n; i++) {
 
            // for loop to keep track of
            // number of columns
            for (int j = 1; j <= n; j++) {
 
                // if last row or first column
                if (i == n || j == 1)
                    Console.Write(" "
                                  + "*");
 
                // to skip the lower triangular
                // part
                else if (j < k)
                    Console.Write(" "
                                  + " ");
 
                // to print star in remaining
                // portion
                else
                    Console.Write(" "
                                  + "*");
            }
            Console.WriteLine();
            k++;
        }
    }
 
    // driver code
    public static void Main()
    {
        int n = 8;
 
        print_pattern(n);
    }
}
 
// This code is contributed by vt_m

PHP

<?php
// PHP implementation of hollow
// lower triangular pattern
 
// Function to print the pattern
function print_pattern($n)
{
    $k = 1;
 
    // for loop to keep track of
    // number of rows
    for ($i = 1; $i <= $n; $i++)
    {
 
        // for loop to keep track of
        // number of columns
        for ($j= 1; $j<= $n; $j++)
        {
 
            // if last row or
            // first column
            if ($i == $n || $j== 1)
                echo " *";
 
            // to skip the lower
            // triangular part
            else if ($j < $k)
                echo "  ";
 
            // to print star in
            // remaining portion
            else
                echo " *";
        }
        echo "\n";
        $k++;
    }
}
 
// Driver code
$n = 8;
print_pattern($n);
 
// This code is contributed by Mithun Kumar
?>

Javascript

<script>
      // JavaScript implementation of hollow
      // lower triangular pattern
 
      // function to print the pattern
      function print_pattern(n) {
        var k = 1;
 
        // for loop to keep track of
        // number of rows
        for (var i = 1; i <= n; i++)
        {
         
          // for loop to keep track of
          // number of columns
          for (var j = 1; j <= n; j++)
          {
           
            // if last row or first column
            if (i == n || j == 1) document.write("  " + "*");
             
            // to skip the lower triangular part
            else if (j < k) document.write("  " + "  ");
             
            // to print star in remaining portion
            else document.write("  " + "*");
          }
          document.write("<br>");
          k++;
        }
      }
 
      // driver code
      var n = 8;
      print_pattern(n);
       
      // This code is contributed by rdtank.
    </script>

Producción : 
 

 * * * * * * * *
 * * * * * * * *
 *   * * * * * *
 *     * * * * *
 *       * * * *
 *         * * *
 *           * *
 * * * * * * * *

Complejidad de tiempo: O (n 2 ), donde n es el número de filas en el patrón.

Espacio auxiliar: O (1), ya que no estamos usando ningún espacio adicional.

Publicación traducida automáticamente

Artículo escrito por Sahil_Bansall y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *