请利用嵌套循环输出平行四边形,平行四边形行数为 n,每行的“*”的数目也为 n,n 由 键盘输入。 示例输入: 5 示例输出: ***** ***** ***** ***** *****C语言代码
时间: 2023-08-31 12:08:58 浏览: 69
以下是C语言代码实现:
```
#include <stdio.h>
int main() {
int n;
printf("请输入平行四边形的行数:");
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
// 输出空格
for(int j = 1; j <= n - i; j++) {
printf(" ");
}
// 输出星号
for(int k = 1; k <= n; k++) {
printf("*");
}
printf("\n");
}
return 0;
}
```
运行结果:
```
请输入平行四边形的行数:5
*****
*****
*****
*****
*****
```
相关问题
请利用嵌套循环输出平行四边形,平行四边形行数为 n,每行的“*”的数目也为 n,n 由 键盘输入。 示例输入: 5 示例输出: ***** ***** ***** ***** *****
可以使用两个嵌套的for循环来输出平行四边形,外层循环控制行数,内层循环控制每行的“*”数目和空格数目。
以下是实现代码:
```python
n = int(input("请输入平行四边形行数:"))
for i in range(n):
# 输出空格
for j in range(n-i-1):
print(" ", end="")
# 输出星号
for k in range(n):
print("*", end="")
print() # 换行
```
输出结果:
```
请输入平行四边形行数:5
*****
*****
*****
*****
*****
```
请利用嵌套循环输出平行四边形
可以使用两个嵌套循环,一个循环用于控制行数,另一个循环用于控制每行输出的空格和星号数量。
下面是一个示例代码:
```
#include <stdio.h>
int main()
{
int rows, cols;
printf("请输入平行四边形的行数和列数:");
scanf("%d %d", &rows, &cols);
for (int i = 1; i <= rows; i++) {
// 输出空格
for (int j = 1; j <= rows - i; j++) {
printf(" ");
}
// 输出星号
for (int j = 1; j <= cols; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
```
输入行数和列数后,输出一个平行四边形,例如:
```
请输入平行四边形的行数和列数:5 10
**********
**********
**********
**********
**********
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)