问题描述 输入整数 n,打印由 n 行星号(*)组成的三角形。当 n 为5时,输出的三角形如下: ***** **** *** ** * 输入形式 一个整数 n。 输出形式 由 n 行星号(*)组成的三角形(形式见样例)。
时间: 2023-11-25 09:50:38 浏览: 74
以下是C语言实现的代码,可以根据输入的整数n打印出由n行星号组成的三角形:
```c
#include <stdio.h>
int main() {
int n,i,j;
printf("请输入一个整数:");
scanf("%d",&n);
for(i = 1; i <= n; i++) {
for(j = 1; j <= n-i; j++)
printf(" ");
for(j = 1; j <= i; j++)
printf("* ");
printf("\n");
}
return 0;
}
```
输入整数n后,程序会使用两个for循环来打印三角形,第一个for循环控制行数,第二个for循环控制每行星号的数量和空格的数量。具体来说,第二个for循环中,第一个循环打印空格,第二个循环打印星号。
相关问题
输入整数 n,打印由 n 行星号(*)组成的三角形。当 n 为5时,输出的三角形如下: ***** **** *** ** *c语言
是的,这是一段C语言代码,用于输入整数n,打印由n行星号(*)组成的三角形。当n为5时,输出的三角形如下:
```
*****
****
***
**
*
```
以下是C语言代码:
```c
#include <stdio.h>
int main() {
int n,i,j;
printf("请输入一个整数:");
scanf("%d",&n);
for(i = 1; i <= n; i++) {
for(j = 1; j <= n-i; j++)
printf(" ");
for(j = 1; j <= i; j++)
printf("* ");
printf("\n");
}
return 0;
}
```
在C语言中,输入整数 n,打印由 n 行星号(*)组成的三角形。当 n 为5时,输出的三角形如下: ***** **** *** ** *
以下是C语言实现输入整数n,打印由n行星号组成的三角形的代码,当n为5时,输出的三角形如下:
```c
#include <stdio.h>
int main() {
int n;
scanf("%d", &n); // 从键盘输入 n。
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
printf(" ");
}
for (int k = 1; k <= i; k++) {
printf("*");
}
printf("\n");
}
return 0;
}
```
输出结果:
```
*
**
***
****
*****
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)