程序说明
金字塔是通过连接多边形底面和称为顶点的点形成的多面体。每个底边和顶点形成一个三角形,称为侧面。它是一个底面为多边形的圆锥体。具有 n 边底的金字塔有 n + 1 个顶点、n + 1 个面和 2n 个边。所有金字塔都是自对偶的。
算法
'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>");
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;
}</p>