2.5.1习题
习题2-3 倒三角形
Enter a positive interger n<=20,output an n-level inverted triangle(output an inverted triangle with n layers).For example,when n Equals 5,the output is as follows
#########
#######
#####
###
#
As can be seen from the graph,each layer corresponds to 2n-1(the number of layers is 2n-1)
#include<stdio.h>
int mian()
{ int n;
int i;
scanf("%d",&n);
for(i=n;i>0;i--)
{ for(j=2*i-1;j>0;j--)
printf("#");
printf("\n");
}
return 0;
}
In order to output the triangle pattern,the full code is as follows
#include<stdio.h>
int main()
{
int n;
int j;
scanf("%d",&n);
int i;
int k;
for(i=n;i>0;i--)
{
for(k=n-i;k>0;k--){
printf(" ");
}
for(j=2*i-1;j>0;j--)
printf("#");
printf("\n");
}
return 0;
}