首頁 >後端開發 >C++ >C語言中列印實心和空心正方形圖案的程序

C語言中列印實心和空心正方形圖案的程序

PHPz
PHPz轉載
2023-09-03 13:37:041617瀏覽

程式描述

在幾何學中,正方形是一種正規四邊形,意味著它有四條相等的邊和四個相等的角。

實心與空心正方形將如下所示

C語言中列印實心和空心正方形圖案的程序

演算法

對於實心正方形-

#
Accept the Number of Rows from the user to draw the Solid Square
For each Row, Print * for each Column to draw the Solid Square

對於空心正方形−

Accept the Number of Rows from the user to draw the Hollow Square
For the First and Last Row, Print * for each Column
For the Remaining Rows, Print * for the first and Last Column.

Example

的中文翻譯為:

範例

/* Program to print hollow and Solid Square pattern */
#include <stdio.h>
int main(){
   int r, c, rows; //Hollow Rhombus
   int r1,c1, rows1; //Solid Rhombus
   clrscr();
   /* Hollow Square */
   printf("Enter the Number of rows for Hollow Square: ");
   scanf("%d", &rows);
   printf("</p><p>");
   for (r=1; r<=rows; r++){
      if (r==1 || r==rows){
         for (c=1; c<=rows; c++){
            printf("*");
         }
      }
      else{
         for (c=1; c<=rows; c++){
            if (c==1 || c==rows){
               printf("*");
            }
            else{
               printf(" ");
            }
         }
      }
      printf("</p><p>");
   }
   printf("</p><p>");
   /* Solid Square */
   printf("Enter the Number of rows for Solid Square: ");
   scanf("%d", &rows1);
   printf("</p><p>");
   for (r1=1; r1<=rows1; r1++){
      for (c1=1; c1<=rows1; c1++){
         printf("*");
      }
      printf("</p><p>");
   }
   getch();
   return 0;
}

輸出

C語言中列印實心和空心正方形圖案的程序

以上是C語言中列印實心和空心正方形圖案的程序的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除