打印实心和空心方形图案的程序
对于任何给定的数字 n,打印用星星 (*) 制成的空心和实心正方形和菱形。
例子:
Input : n = 4
Output :
Solid Square:
****
****
****
****
Hollow Square:
****
* *
* *
****
1.实心正方形:实心正方形是所有给定图案中最简单的。要打印 n 行的实心正方形,我们应该使用两个循环都迭代 n 次。其中外循环用于行数,内循环用于打印特定行中的所有星星。
2. 空心广场:空心广场需要一点即兴发挥。在这里,我们应该再次使用两个循环,其中第一行和最后一行必须包含所有 n 颗星,其余行仅在第一列和最后一列中有星号。
C++
// C++ program to print
// hollow and solid square patterns
#include
using namespace std;
// Function for hollow square
void hollowSquare(int rows)
{
int i, j;
for (i=1; i<=rows; i++)
{
// Print stars for each solid rows
if (i==1 || i==rows)
for (j=1; j<=rows; j++)
cout << "*";
// stars for hollow rows
else
for (j=1; j<=rows; j++)
if (j==1 || j==rows)
cout << "*";
else
cout << " ";
// Move to the next line/row
cout << "\n";
}
}
// Function for Solid square
void solidSquare(int rows)
{
int i, j;
for (i=1; i<=rows; i++)
{
// Print stars after spaces
for (j=1; j<=rows; j++)
cout << "*";
// Move to the next line/row
cout << "\n";
}
}
// Utility program to print all patterns
void printPattern(int rows)
{
cout << "\nSolid Square:\n";
solidSquare(rows);
cout << "\nHollow Square:\n";
hollowSquare(rows);
}
// Driver program
int main()
{
int rows = 5;
printPattern (rows);
return 0;
}
Java
// Java program to print
// hollow and solid square patterns
class GFG
{
// Function for hollow square
static void hollowSquare(int rows)
{
int i, j;
for (i = 1; i <= rows; i++)
{
// Print stars for each solid rows
if (i == 1 || i == rows)
for (j = 1; j <= rows; j++)
System.out.print("*");
// stars for hollow rows
else
for (j = 1; j <= rows; j++)
if (j == 1 || j == rows)
System.out.print("*");
else
System.out.print(" ");
// Move to the next line/row
System.out.print("\n");
}
}
// Function for Solid square
static void solidSquare(int rows)
{
int i, j;
for (i = 1; i <= rows; i++)
{
// Print stars after spaces
for (j = 1; j <= rows; j++)
System.out.print("*");
// Move to the next line/row
System.out.print("\n");
}
}
// Utility program to print all patterns
static void printPattern(int rows)
{
System.out.print("\nSolid Square:\n");
solidSquare(rows);
System.out.print("\nHollow Square:\n");
hollowSquare(rows);
}
// Driver program
public static void main (String[] args)
{
int rows = 5;
printPattern (rows);
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python3 program to print
# hollow and solid square patterns
# Function for hollow square
def hollowSquare(rows):
for i in range(1, rows + 1):
# Prstars for each solid rows
if (i == 1 or i == rows):
for j in range(1, rows + 1):
print("*", end = "")
# stars for hollow rows
else:
for j in range(1, rows + 1):
if (j == 1 or j == rows):
print("*", end = "")
else:
print(end = " ")
# Move to the next line/row
print()
# Function for Solid square
def solidSquare(rows):
for i in range(1, rows):
# Print stars after spaces
for j in range(1, rows + 1):
print("*", end = "")
# Move to the next line/row
print()
# Utility program to print all patterns
def printPattern(rows):
print("Solid Square:")
solidSquare(rows)
print("\nHollow Square:")
hollowSquare(rows)
# Driver Code
rows = 5
printPattern (rows)
# This code is contributed by
# Mohit kumar 29
C#
// C# program to print
// hollow and solid square patterns
using System;
class GFG
{
// Function for hollow square
static void hollowSquare(int rows)
{
int i, j;
for (i = 1; i <= rows; i++)
{
// Print stars for each solid rows
if (i == 1 || i == rows)
for (j = 1; j <= rows; j++)
Console.Write("*");
// stars for hollow rows
else
for (j = 1; j <= rows; j++)
if (j == 1 || j == rows)
Console.Write("*");
else
Console.Write(" ");
// Move to the next line/row
Console.WriteLine();
}
}
// Function for Solid square
static void solidSquare(int rows)
{
int i, j;
for (i = 1; i <= rows; i++)
{
// Print stars after spaces
for (j = 1; j <= rows; j++)
Console.Write("*");
// Move to the next line/row
Console.WriteLine();
}
}
// Utility program to print all patterns
static void printPattern(int rows)
{
Console.Write("\nSolid Square:\n");
solidSquare(rows);
Console.Write("\nHollow Square:\n");
hollowSquare(rows);
}
// Driver program
public static void Main ()
{
int rows = 5;
printPattern (rows);
}
}
// This code is contributed by vt_m.
PHP
Javascript
输出 :
Solid Square:
*****
*****
*****
*****
*****
Hollow Square:
*****
* *
* *
* *
*****