Programa para imprimir patrones de estrellas rectangulares o cuadradas huecas

Patrón de estrella de rectángulo hueco:

La tarea es imprimir debajo del patrón hueco de la dimensión dada. 
 

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

Explicación: 
 

  • Ingrese el número de filas y columnas.
  • Para filas de rectángulo, ejecute el bucle exterior de 1 a filas. 
     
for (i = 1; i < = rows; i++)
  • Para la columna del rectángulo, ejecute el ciclo interno de 1 a columnas. 
     
for (j = 1; j < = columns; j++)
  • Escriba una estrella para la primera o la última fila o para la primera o la última columna; de lo contrario, escriba un espacio en blanco.
  • Después de imprimir todas las columnas de una fila, imprime una nueva línea después del ciclo interno.

C++

// C++ code for hollow rectangle
#include <bits/stdc++.h>
using namespace std;
 
// Function to print hollow rectangle
void print_rectangle(int n, int m)
{
    int i, j;
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= m; j++)
        {
            if (i == 1 || i == n ||
                j == 1 || j == m)        
                cout << "*";            
            else
                cout << " ";
        }
        cout << endl;
    }
 
}
 
// Driver Code
int main()
{
    int rows = 6, columns = 20;
    print_rectangle(rows, columns);
    return 0;
}
 
// This code is contributed
// by rathbhupendra

C

// C code for hollow rectangle
#include <stdio.h>
 
// Function to print hollow rectangle
void print_rectangle(int n, int m)
{
    int i, j;
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= m; j++)
        {
            if (i==1 || i==n || j==1 || j==m)           
                printf("*");           
            else
                printf(" ");           
        }
        printf("\n");
    }
 
}
 
// Driver program for above function
int main()
{
    int rows = 6, columns = 20;
    print_rectangle(rows, columns);
    return 0;
}

Java

// JAVA code for hollow rectangle
import java.io.*;
 
class GFG {
     
    // Function to print hollow rectangle
    static void print_rectangle(int n, int m)
    {
        int i, j;
        for (i = 1; i <= n; i++)
        {
            for (j = 1; j <= m; j++)
            {
                if (i == 1 || i == n ||
                    j == 1 || j == m)           
                    System.out.print("*");           
                else
                    System.out.print(" ");           
            }
            System.out.println();
        }
      
    }
      
    // Driver program for above function
    public static void main(String args[])
    {
        int rows = 6, columns = 20;
        print_rectangle(rows, columns);
    }
}
 
/*This code is contributed by Nikita Tiwari.*/

Python3

# Python 3 code for hollow rectangle
 
# Function to print hollow rectangle
def print_rectangle(n, m) :
     
    for i in range(1, n+1) :
        for j in range(1, m+1) :
            if (i == 1 or i == n or
                j == 1 or j == m) :
                print("*", end="")           
            else :
                print(" ", end="")           
         
        print()
 
 
# Driver program for above function
rows = 6
columns = 20
print_rectangle(rows, columns)
 
 
# This code is contributed by Nikita Tiwari.

PHP

<?php
// PHP code for hollow rectangle
 
// Function to print hollow rectangle
function print_rectangle($n, $m)
{
    $i;
    $j;
    for ($i = 1; $i <= $n; $i++)
    {
        for ($j = 1; $j <= $m; $j++)
        {
            if ($i == 1 || $i == $n ||
                $j == 1 || $j == $m)        
                echo("*");        
            else
                echo(" ");        
        }
        echo("\n");
    }
 
}
 
    // Driver Code
    $rows = 6;
    $columns = 20;
    print_rectangle($rows, $columns);
 
// This code is contributed by nitin mittal
?>

Javascript

<script>
      // JavaScript code for hollow rectangle
      // Function to print hollow rectangle
      function print_rectangle(n, m)
      {
        var i, j;
        for (i = 1; i <= n; i++)
        {
          for (j = 1; j <= m; j++)
          {
            if (i == 1 || i == n || j == 1 || j == m)
                 document.write("*");
            else
                document.write("  ");
          }
          document.write("<br>");
        }
      }
 
      // Driver Code
      var rows = 6,
        columns = 20;
      print_rectangle(rows, columns);
       
      // This code is contributed by rdtank.
    </script>

Producción: 
 

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

Complejidad de tiempo: O(m * n), donde m y n representan las entradas dadas.
Espacio auxiliar: O(1), no se requiere espacio adicional, por lo que es una constante.

Patrón de estrella cuadrada hueca:

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

Explicación: 
 

  • Ingrese el número de filas.
  • Para filas, un bucle exterior de 1 a N.
  • Para columnas, un bucle interno de 1 a N
  • Estrella de impresión de bucle interior interior para la primera y la última fila o para la primera y la última columna. que es estrella impresa 
     
 if i == 1 or i == N or j == 1 or j == N
  • de lo contrario imprimir espacio.
  • Después de imprimir todas las columnas de una fila, imprima una línea en blanco después del ciclo interno. 
     

C++

// C++ code for hollow squares
#include <bits/stdc++.h>
using namespace std;
 
// Function to print hollow square
void print_square(int n)
{
    int i, j;
     
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= n; j++)
        {
            if (i==1 || i==n || j==1 || j==n)    
                cout << "*";        
            else   
                cout << " ";        
        }
        cout << "\n";
    }
 
}
 
// Driver code for above function
int main()
{
    int rows = 8;
    print_square(rows);
    return 0;
}
 
// This code is contributed by Code_Mech

C

// C code for hollow squares
#include <stdio.h>
 
// Function to print hollow square
void print_square(int n)
{
    int i, j;
     
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= n; j++)
        {
            if (i==1 || i==n || j==1 || j==n)           
                printf("*");           
            else           
                printf(" ");           
        }
        printf("\n");
    }
 
}
 
// Driver code for above function
int main()
{
    int rows = 8;
    print_square(rows);
    return 0;
}

Java

// JAVA code for hollow squares
import java.io.*;
 
class GFG {
     
    // Function to print hollow square
    static void print_square(int n)
    {
        int i, j;
          
        for (i = 1; i <= n; i++)
        {
            for (j = 1; j <= n; j++)
            {
                if (i == 1 || i == n ||
                    j == 1 || j == n)           
                    System.out.print("*");           
                else          
                    System.out.print(" ");           
            }
            System.out.println();
        }
      
    }
      
    // Driver code for above function
    public static void main(String args[])
    {
        int rows = 8;
        print_square(rows);
    }
}
 
/*This code is contributed by Nikita Tiwari.*/

Python3

# Python 3 code for hollow squares
 
# Function to print hollow square
def print_square(n) :
     
    for i in range(1, n+1) :
        for j in range(1, n+1) :
            if (i == 1 or i == n or
                j == 1 or j == n) :           
                print("*", end = "")           
            else :
                print(" ", end = "")           
         
        print()
         
         
  
# Driver code for above function
rows = 8
print_square(rows)
 
# This code is contributed by Nikita Tiwari.

C++

// C++ program to print hollow square star
// pattern with diagonal
#include <bits/stdc++.h>
using namespace std;
 
// Function to print hollow square with
// primary and secondary diagonal
void print_squaredi(int n)
{
    int i, j;
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= n; j++)
        {
            if (i == 1 || i == n || j == 1 ||
                j == n || i == j || j == (n - i + 1))        
                cout << "*";        
            else       
                cout << " ";        
        }
        cout << "\n";
    }
}
 
// Driver code
int main()
{
    int rows = 8;
    print_squaredi(rows);
    return 0;
}
 
// This code is contributed by SHUBHAMSINGH10

C

// C program to print hollow square star
// pattern with diagonal
#include <stdio.h>
 
// Function to print hollow square with
// primary and secondary diagonal
void print_squaredi(int n)
{
    int i, j;
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= n; j++)
        {
            if (i==1 || i==n || j==1 ||
                j==n || i==j || j==(n - i + 1))           
                printf("*");           
            else           
                printf(" ");           
        }
        printf("\n");
    }
 
}
// Driver code for above function
int main()
{
    int rows = 8;
    print_squaredi(rows);
    return 0;
}

Java

// JAVA program to print hollow square
// star pattern with diagonal
import java.io.*;
 
class GFG {
     
    // Function to print hollow square with
    // primary and secondary diagonal
    static void print_squaredi(int n)
    {
        int i, j;
        for (i = 1; i <= n; i++)
        {
            for (j = 1; j <= n; j++)
            {
                if (i == 1 || i == n || j == 1 ||
                j == n || i == j || j == (n - i + 1))           
                    System.out.print("*");           
                else          
                    System.out.print(" ");           
            }
            System.out.println();
        }
      
    }
     
    // Driver code for above function
    public static void main(String args[])
    {
        int rows = 8;
        print_squaredi(rows);
    }
}
 
/*This code is contributed by Nikita Tiwari.*/

Python3

# Python 3 program to print hollow
# square star pattern with diagonal
 
# Function to print hollow square with
# primary and secondary diagonal
def print_squaredi(n) :
    for i in range(1, n+1) :
        for j in range(1, n+1) :
            if (i==1 or i==n or j==1 or j==n
                or i==j or j==(n - i + 1)) :
                print("*", end = "")           
            else :
                print(" ", end = "")           
         
        print()
         
         
# Driver code for above function
rows = 8
print_squaredi(rows)
 
# This code is contributed by Nikita Tiwari.

C#

// C# program to print hollow square
// star pattern with diagonal
using System;
 
class GFG {
     
    // Function to print hollow square with
    // primary and secondary diagonal
    static void print_squaredi(int n)
    {
        int i, j;
        for (i = 1; i <= n; i++)
        {
            for (j = 1; j <= n; j++)
            {
                if (i == 1 || i == n ||
                    j == 1 || j == n ||
                    i == j || j == (n - i + 1))        
                    Console.Write("*");        
                else       
                Console.Write(" ");        
            }
            Console.WriteLine();
        }
     
    }
     
    // Driver code
    public static void Main()
    {
        int rows = 8;
        print_squaredi(rows);
    }
}
 
// This code is contributed by Nitin Mittal.

PHP

<?php
// PHP program to print hollow square
// star pattern with diagonal
 
// Function to print hollow square with
// primary and secondary diagonal
function print_squaredi($n)
{
    $i;
    $j;
    for($i = 1; $i <= $n; $i++)
    {
        for ($j = 1; $j <= $n; $j++)
        {
            if($i == 1 or $i == $n or $j == 1 ||
               $j == $n or $i == $j or $j == ($n -
                                          $i + 1))    
                printf("*");        
            else   
                echo " ";    
        }
        echo "\n";
    }
 
}
 
    // Driver Code
    $rows = 8;
    print_squaredi($rows);
 
// This code is contributed by anuj_67.
?>

Producción: 
 

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

Complejidad de tiempo: O(n 2 ), donde n representa la entrada dada.
Espacio auxiliar: O(1), no se requiere espacio adicional, por lo que es una constante.

Publicación traducida automáticamente

Artículo escrito por Shivani Baghel 1 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 *