c语言输出一个空心棱形
时间: 2023-05-23 12:07:00 浏览: 103
下面是C语言输出一个空心棱形的代码:
```c
#include <stdio.h>
int main()
{
int i, j, k, n;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n-i; j++)
{
printf(" ");
}
for (k = 1; k <= 2*i-1; k++)
{
if (k == 1 || k == 2*i-1 || i == n)
printf("*");
else
printf(" ");
}
printf("\n");
}
for (i = n-1; i >= 1; i--)
{
for (j = 1; j <= n-i; j++)
{
printf(" ");
}
for (k = 1; k <= 2*i-1; k++)
{
if (k == 1 || k == 2*i-1 || i == n)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
```
输入示例:
```
Enter the number of rows: 5
```
输出示例:
```
*
* *
* *
* *
* *
* *
* *
* *
*
```
阅读全文