Home  >  Article  >  Backend Development  >  Teach you step by step how to print an inverted triangle in C language

Teach you step by step how to print an inverted triangle in C language

烟雨青岚
烟雨青岚forward
2020-07-07 11:51:4312756browse

Teach you step by step how to print an inverted triangle in C language

When you first learn the C language, everyone will come into contact with the compilation triangle. This article describes the compilation code of the inverted triangle. Let’s take a look.

C language code for printing inverted triangles:

#include<stdio.h>
void main()
{ 
int i,j,k,l;//i为第几层星号;k为星号前的空格数;j为星号数;l为打印的行数;
printf("请输入需要打印的行数:\n");
scanf("%d",&l);
printf("打印的图案为:\n");
for(i=1;i<=l;i++)
{
for(k=1;k<=i-1;k++)
printf(" ");//输出空格
for(j=1;j<=2*(l-i)+1;j++)//输出星号;
 /*每层2*l-1为每行的星号数;
每层2*(i-1)为星号前的空格数;
星号数-空格数==所在行星号数;*/
printf("*");
printf("\n");//输出空格,换行输出每行的星号;
}
}/*抓住三点:
1、第几层即i栏;
2、每层的空格数即k栏;
3、每层的星号数即j栏;
     */

Thank you for reading, have you learned it?

This article is reproduced from: https://blog.csdn.net/klj13969902329/article/details/80436030

Recommended tutorial: "C Language"

The above is the detailed content of Teach you step by step how to print an inverted triangle in C language. For more information, please follow other related articles on the PHP Chinese website!

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