用if语句实现 输入4个不同的整数并输出其中最小的数
时间: 2024-06-02 08:13:58 浏览: 6
num1 = int(input("请输入第一个整数:"))
num2 = int(input("请输入第二个整数:"))
num3 = int(input("请输入第三个整数:"))
num4 = int(input("请输入第四个整数:"))
min_num = num1 # 假设第一个数为最小值
if num2 < min_num:
min_num = num2
if num3 < min_num:
min_num = num3
if num4 < min_num:
min_num = num4
print("最小的数是:", min_num)
相关问题
只使用if语句编程实现输入三个整数输出其中的最大数
以下是使用if语句编程实现输入三个整数输出其中的最大数的代码:
```
#include <stdio.h>
int main() {
int a, b, c;
printf("请输入三个整数:\n");
scanf("%d %d %d", &a, &b, &c);
if (a >= b && a >= c) {
printf("最大数为:%d\n", a);
} else if (b >= a && b >= c) {
printf("最大数为:%d\n", b);
} else {
printf("最大数为:%d\n", c);
}
return 0;
}
```
用if语句写输入4个整数,从大到小输出
A:
```
a = int(input('请输入第一个整数:'))
b = int(input('请输入第二个整数:'))
c = int(input('请输入第三个整数:'))
d = int(input('请输入第四个整数:'))
if a >= b and a >= c and a >= d:
max_num = a
if b >= c and b >= d:
print(max_num, b, c, d)
elif c >= b and c >= d:
print(max_num, c, b, d)
else:
print(max_num, d, b, c)
elif b >= a and b >= c and b >= d:
max_num = b
if a >= c and a >= d:
print(max_num, a, c, d)
elif c >= a and c >= d:
print(max_num, c, a, d)
else:
print(max_num, d, a, c)
elif c >= a and c >= b and c >= d:
max_num = c
if a >= b and a >= d:
print(max_num, a, b, d)
elif b >= a and b >= d:
print(max_num, b, a, d)
else:
print(max_num, d, a, b)
else:
max_num = d
if a >= b and a >= c:
print(max_num, a, b, c)
elif b >= a and b >= c:
print(max_num, b, a, c)
else:
print(max_num, c, a, b)
```
说明:先用四个input语句分别输入四个整数,然后用if语句和比较操作符比较大小,找出其中最大值,再用嵌套的if语句将剩余三个数从大到小排序并输出。
相关推荐
![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)
![](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)