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.

示例

/* 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("
");    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("
");    }    printf("
");    /* Solid Square */    printf("Enter the Number of rows for Solid Square: ");    scanf("%d", &rows1);    printf("
");    for (r1=1; r1<=rows1; r1++){       for (c1=1; c1<=rows1; c1++){          printf("*");       }       printf("
");    }    getch();    return 0; }

輸出

更新於: 2020-07-13

瀏覽數 698

開啟您的 職業

完成課程並獲取認證

開始
廣告