📜  打印矩形图案的程序

📅  最后修改于: 2022-05-13 01:57:59.370000             🧑  作者: Mango

打印矩形图案的程序

给定高度 h 和宽度 w,打印一个矩形图案,如下例所示。

例子:

Input : h = 4, w = 5
Output : @@@@@
         @   @
         @   @
         @@@@@

Input : h = 7, w = 9
Output : @@@@@@@@
         @      @
         @      @
         @      @
         @      @
         @      @
         @@@@@@@@

这个想法是运行两个循环。一个代表要打印的行数,另一个代表列数。仅当当前行是第一行或最后一行时才打印“@”。或当前列是第一个或最后一个。

C++
// CPP program to print a rectangular pattern
#include
using namespace std;
  
void printRectangle(int h, int w)
{
    for (int i=0; i


Java
// JAVA program to print a rectangular 
// pattern
  
class GFG {
      
    static void printRectangle(int h, int w)
    {
        for (int i = 0; i < h; i++)
        {
            System.out.println();
            for (int j = 0; j < w; j++)
            {
                // Print @ if this is first 
                // row or last row. Or this
                // column is first or last.
                if (i == 0 || i == h-1 ||
                    j== 0 || j == w-1)
                   System.out.print("@");
                else
                   System.out.print(" ");
            }
        }
    }
       
    // Driver code
    public static void main(String args[])
    {  
        int h = 4, w = 5;
        printRectangle(h, w) ;
    }
}
  
/*This code is contributed by Nikita Tiwari.*/


Python3
# Python 3 program to print a rectangular
# pattern
  
def printRectangle(h, w) :
    for i in range(0, h) :
        print ("")
        for j in range(0, w) :
            # Print @ if this is first row
            # or last row. Or this column
            # is first or last.
            if (i == 0 or i == h-1 or j== 0 or j == w-1) :
                print("@",end="")
            else :
                print(" ",end="")
          
   
# Driver code
h = 4
w = 5
printRectangle(h, w) 
  
# This code is contributed by Nikita Tiwari.


C#
// C# program to print a rectangular 
// pattern
using System;
class GFG {
      
    static void printRectangle(int h, int w)
    {
        for (int i = 0; i < h; i++)
        {
            Console.WriteLine();
            for (int j = 0; j < w; j++)
            {
                // Print @ if this is first 
                // row or last row. Or this
                // column is first or last.
                if (i == 0 || i == h-1 ||
                    j== 0 || j == w-1)
                Console.Write("@");
                else
                Console.Write(" ");
            }
        }
    }
      
    // Driver code
    public static void Main()
    { 
        int h = 4, w = 5;
        printRectangle(h, w) ;
    }
}
  
/*This code is contributed by vt_m.*/


PHP



输出:
@@@@@
@   @
@   @
@@@@@