首页 > 文章列表 > 在C语言中编写一个打印金字塔图案的程序

在C语言中编写一个打印金字塔图案的程序

c语言 打印 金字塔
124 2023-09-06

程序说明

金字塔是通过连接多边形底面和称为顶点的点形成的多面体。每个底边和顶点形成一个三角形,称为侧面。它是一个底面为多边形的圆锥体。具有 n 边底的金字塔有 n + 1 个顶点、n + 1 个面和 2n 个边。所有金字塔都是自对偶的。

在C语言中编写一个打印金字塔图案的程序< /p>

算法

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("

");    printf("The Pyramid Pattern for the number of rows are:");    printf("

");    for(r=1;r<=rows;++r,t=0) {       for(s=1; s<=rows-r; ++s){          printf(" ");       }       while (t!=2*r-1) {          printf("* ");          ++t;       }       printf("

");    }    getch();    return 0; }

输出

在C语言中编写一个打印金字塔图案的程序