Home > Backend Development > C#.Net Tutorial > 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

烟雨青岚
Release: 2020-07-07 11:51:43
forward
12865 people have browsed it

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栏;
     */
Copy after login

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!

Related labels:
source:csdn.net
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