ホームページ  >  記事  >  バックエンド開発  >  C言語でベタと中空の正方形パターンを印刷するプログラム

C言語でベタと中空の正方形パターンを印刷するプログラム

PHPz
PHPz転載
2023-09-03 13:37:041455ブラウズ

プログラムの説明

幾何学では、正方形は正四角形であり、4 つの等しい辺と 4 つの等しい角があることを意味します。

実線の正方形と中空の正方形は次のようになります。

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

中国語への翻訳は次のとおりです:

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;
}

Output

C言語でベタと中空の正方形パターンを印刷するプログラム

以上がC言語でベタと中空の正方形パターンを印刷するプログラムの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はtutorialspoint.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。