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

Write a program in C language to print a pyramid pattern

王林
Release: 2023-09-06 19:13:05
forward
1735 people have browsed it

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.
Copy after login

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;
}
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template