ホームページ  >  記事  >  バックエンド開発  >  ピラミッドパターンを出力するプログラムを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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はtutorialspoint.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。