首頁  >  文章  >  後端開發  >  在C語言中編寫一個列印金字塔圖案的程序

在C語言中編寫一個列印金字塔圖案的程序

王林
王林轉載
2023-09-06 19:13:051631瀏覽

程式說明

金字塔是透過連接多邊形底面和稱為頂點的點所形成的多面體。每個底邊和頂點形成一個三角形,稱為側面。它是一個底面為多邊形的圓錐體。有 n 邊底的金字塔有 n 1 個頂點、n 1 個面和 2n 個邊。所有金字塔都是自對偶的。

在C語言中編寫一個列印金字塔圖案的程序

演算法

Accept the number of rows from the user to form pyramid shape
Iterate the loop till the number of rows specified by the user:
Display 1 star in the first row
Increase the number of stars based on the number of rows.

範例

/*Program to print Pyramid Pattern*/
#include<stdio.h>
int main() {
   int r, s, rows=0;
   int t=0;
   clrscr();
   printf("Enter number of rows to print the pyramid: ");
   scanf("%d", &rows);
   printf("</p><p>");
   printf("The Pyramid Pattern for the number of rows are:");
   printf("</p><p></p><p>");
   for(r=1;r<=rows;++r,t=0) {
      for(s=1; s<=rows-r; ++s){
         printf(" ");
      }
      while (t!=2*r-1) {
         printf("* ");
         ++t;
      }
      printf("</p><p>");
   }
   getch();
   return 0;
}

輸出

在C語言中編寫一個列印金字塔圖案的程序

以上是在C語言中編寫一個列印金字塔圖案的程序的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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