C語言列印映象空心平行四邊形程式


程式描述

它是一個四邊形,其中兩對對邊平行。

平行四邊形有六個重要的性質需要注意

  • 對邊全等 (AB = DC)。
  • 對角相等 (D = B)。
  • 鄰角互補 (A + D = 180°)。
  • 如果一個角是直角,則所有角都是直角。
  • 平行四邊形的對角線互相平分。
  • 平行四邊形的每條對角線都將其分成兩個全等的

演算法

  • 從使用者處接收行數和列數。將其儲存在rows和cols變數中。
  • 要遍歷行,執行一個外迴圈,迴圈結構應類似於`for(r=1; r<=rows; r++)`。
  • 要列印空格,執行一個內迴圈,迴圈結構為`for(c=1; c
  • 列印星號以形成空心平行四邊形,執行另一個內迴圈,迴圈結構類似於`for(c=1; c<=cols; c++)`。在此迴圈內,僅當`r==1`或`r==rows`或`c==1`或`c==cols`時才打印星號。
  • 列印完一行的所有列後,換行,即列印換行符。

示例

// C program to print mirrored hollow parallelogram
#include <stdio.h>
int main(){
   int rows,cols,r,c;
   clrscr(); /*Clears the Screen*/
   printf("Please enter the number of Rows: ");
   scanf("%d", &rows);
   printf("
");    printf("Please enter the number of Columns: ");    scanf("%d", &cols);    printf("
");    printf("The Mirrored Hollow Parallelogram is: ");    printf("
");    for(r = 1; r <= rows; r++){       // Display spaces       for(c = 1; c < r; c++) {          printf(" ");       }       // Display hollow parallelogram       for(c = 1; c <= cols; c++) {          if (r == 1 || r == rows || c == 1 || c == cols) {             printf("*");          }          else {             printf(" ");          }       }       printf("
");    }    getch();    return 0; }

輸出


更新於:2020年1月9日

559 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.