用c++输入长和宽,输出矩形
时间: 2023-05-26 12:07:54 浏览: 901
以下是使用 C 语言编写的程序,用于输入长和宽,然后输出对应的矩形:
```c
#include <stdio.h>
int main() {
int width, height;
printf("请输入矩形的长和宽:\n");
scanf("%d %d", &width, &height);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
```
该程序使用 `scanf()` 函数获取用户输入的 `width` 和 `height`,然后使用双重循环打印 `width` 行、`height` 列的矩形,每行输出 `width` 个星号(*)。在每行输出后,使用 `printf()` 函数的换行符 `\n` 将光标移动到下一行。
相关问题
用c++输入长和宽,输出矩形简单一些
```
#include <stdio.h>
int main() {
int length, width;
printf("请输入矩形的长和宽:\n");
scanf("%d%d", &length, &width);
printf("矩形的面积是: %d\n", length * width);
printf("矩形的周长是: %d\n", 2 * (length + width));
return 0;
}
```
输入矩形的长和宽,计算其面积输出
可以使用以下代码计算矩形的面积:
```python
length = float(input("请输入矩形的长:"))
width = float(input("请输入矩形的宽:"))
area = length * width
print("矩形的面积为:", area)
```
如果你需要使用其他编程语言,可以参考以下代码:
C++:
```c++
#include <iostream>
using namespace std;
int main() {
float length, width, area;
cout << "请输入矩形的长:";
cin >> length;
cout << "请输入矩形的宽:";
cin >> width;
area = length * width;
cout << "矩形的面积为:" << area << endl;
return 0;
}
```
Java:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
float length, width, area;
Scanner input = new Scanner(System.in);
System.out.print("请输入矩形的长:");
length = input.nextFloat();
System.out.print("请输入矩形的宽:");
width = input.nextFloat();
area = length * width;
System.out.println("矩形的面积为:" + area);
}
}
```
Lua:
```lua
io.write("请输入矩形的长:")
length = io.read("*n")
io.write("请输入矩形的宽:")
width = io.read("*n")
area = length * width
print("矩形的面积为:", area)
```
关于 "lua closure factory 完整代码" 和 "中文加密" 的问题,我可以回答,但需要您提供更具体的问题和背景信息。
相关推荐
![](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)