用c++输出一个空心菱形
时间: 2023-05-29 18:04:57 浏览: 836
以下是使用C语言输出空心菱形的代码:
```c
#include <stdio.h>
int main() {
int rows, i, j, space;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++) {
for (space = 1; space <= rows - i; space++) {
printf(" ");
}
for (j = 1; j <= 2 * i - 1; j++) {
if (j == 1 || j == 2 * i - 1) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
for (i = rows; i >= 1; i--) {
for (space = 1; space <= rows - i; space++) {
printf(" ");
}
for (j = 1; j <= 2 * i - 1; j++) {
if (j == 1 || j == 2 * i - 1) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
```
输出结果:
```
Enter the number of rows: 5
*
* *
* *
* *
* *
* *
* *
* *
*
```
阅读全文