Home  >  Article  >  Backend Development  >  Write a program in C language to print a pyramid pattern

Write a program in C language to print a pyramid pattern

王林
王林forward
2023-09-06 19:13:051686browse

Program description

A pyramid is a polyhedron formed by connecting polygon bases and points called vertices. Each base and vertex form a triangle called a side. It is a cone with a polygonal base. A pyramid with an n-sided base has n 1 vertices, n 1 faces, and 2n sides. All pyramids are self-dual.

Write a program in C language to print a pyramid pattern

Algorithm

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.

Example

/*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;
}

Output

Write a program in C language to print a pyramid pattern

The above is the detailed content of Write a program in C language to print a pyramid pattern. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete