C语言输入整数n,显示星号构成的三角形。例如,n=6时,显示输出的三角形如样例输出所示。 输入格式: 测试数据有多组,处理到文件尾。每组测试输入一个正整数n(0<n<41)。 输出格式: 对于每组测试,输出n行构成的三角形。 输入样例:
时间: 2024-03-24 09:37:21 浏览: 238
以下是 C 语言代码实现:
```c
#include <stdio.h>
int main() {
int n;
while (scanf("%d", &n) != EOF) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n-i; j++) {
printf(" ");
}
for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
}
return 0;
}
```
输入整数 n,循环从 1 到 n,每次输出空格加上星号字符,其中空格数为 n-i,星号数为 i,并在每行结束时输出一个换行符。使用 scanf 函数来输入整数,当输入结束时会返回 EOF,从而跳出循环。
相关问题
题目描述 输入行数,打印一个由星号组成的倒直角三角形。 输入 输入一个正整数n,表示行数。 输出 输出一个由星号组成的倒直角三角形。如样例所示。 样例输入 Copy 5 样例输出 Copy ***** **** *** ** *用c语言回答
题目描述:输入一个正整数n,打印一个由星号组成的倒直角三角形,其中第一行有n个星号,每行星号数量递减1,直到最后一行只有1个星号。
C语言代码如下:
```c
#include <stdio.h>
int main() {
int n;
scanf("%d", &n); // 输入行数
for (int i = n; i >= 1; i--) { // 外层循环控制行数
for (int j = 1; j <= i; j++) { // 内层循环控制每行星号数量
printf("*");
}
printf("\n"); // 每行结束后换行
}
return 0;
}
```
C语言7-2 空心等腰三角形 请编写程序,输入等腰三角形的高,输出由星号勾勒出的等腰三角形。 输入格式 行数 (正整数) 输出格式 星号勾勒出的等腰三角形 (参见输出样例) 输入样例1 1 输出样例1 * 输入样例2 5 输出样例2 * * * * * * * *********
```c
#include <stdio.h>
int main() {
int height, i, j;
scanf("%d", &height);
for (i = 1; i <= height; i++) {
if (i == height) {
for (j = 1; j <= 2 * i - 1; j++) {
printf("*");
}
} else {
for (j = 1; j <= 2 * i - 1; j++) {
if (j == 1 || j == 2 * i - 1) {
printf("*");
} else {
printf(" ");
}
}
}
printf("\n");
}
return 0;
}
```
阅读全文
相关推荐














