编写一个能输出星号塔的小程序。 如果输入是4,则输出的星号塔为:
时间: 2024-04-15 14:28:18 浏览: 18
Sure! Here's a small program in C to generate a star pyramid:
```c
#include <stdio.h>
void printStarPyramid(int rows) {
int i, j, space;
for (i = 1; i <= rows; i++) {
// Print spaces
for (space = 1; space <= rows - i; space++) {
printf(" ");
}
// Print stars
for (j = 1; j <= 2 * i - 1; j++) {
printf("*");
}
printf("\n");
}
}
int main() {
int rows;
printf("请输入行数: ");
scanf("%d", &rows);
printStarPyramid(rows);
return 0;
}
```
When you run this program and enter the number of rows as input (e.g., 4), it will generate the following star pyramid:
```
*
***
*****
*******
```
Each row of the pyramid consists of spaces and asterisks. The number of spaces decreases from top to bottom, while the number of asterisks increases. The above program uses nested loops to achieve this pattern.
I hope this helps! Let me know if you have any other questions.
相关推荐
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)