写一个从小到大输出四个数的c语言程序
时间: 2023-05-20 09:02:03 浏览: 103
以下是一个从小到大输出四个数的C语言程序:
```c
#include <stdio.h>
int main() {
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
if (a > b) {
int t = a;
a = b;
b = t;
}
if (a > c) {
int t = a;
a = c;
c = t;
}
if (a > d) {
int t = a;
a = d;
d = t;
}
if (b > c) {
int t = b;
b = c;
c = t;
}
if (b > d) {
int t = b;
b = d;
d = t;
}
if (c > d) {
int t = c;
c = d;
d = t;
}
printf("%d %d %d %d\n", a, b, c, d);
return 0;
}
```
这个程序通过比较四个数的大小,将它们从小到大排序,然后输出结果。
相关问题
c语言四个任意整数从小到大输出程序
在C语言中,你可以通过嵌套循环或者数组排序的方式来实现四个整数从小到大的输出。这里是一个简单的嵌套循环的例子:
```c
#include <stdio.h>
int main() {
int num1, num2, num3, num4; // 定义四个整数变量
printf("请输入四个整数:\n");
scanf("%d%d%d%d", &num1, &num2, &num3, &num4); // 输入四个整数
// 使用嵌套循环找到并输出最小值
if (num1 < num2) {
if (num1 < num3 && num1 < num4)
printf("最小的是:%d\n", num1);
else if (num2 < num3 && num2 < num4)
printf("最小的是:%d\n", num2);
else if (num3 < num4)
printf("最小的是:%d\n", num3);
else
printf("最小的是:%d\n", num4);
} else if (num2 < num3 && num2 < num4) { // 如果num1不是最小,比较num2
// 类似地,依次比较其他两个数
}
return 0;
}
```
c语言输入四个数从小到大输出
好的,以下是用C语言编写的实现输入四个数从小到大输出的程序:
```c
#include <stdio.h>
int main() {
int a, b, c, d, temp;
printf("请输入四个数:\n");
scanf("%d %d %d %d", &a, &b, &c, &d);
// 比较四个数,使用冒泡排序的思想
if (a > b) {
temp = a;
a = b;
b = temp;
}
if (a > c) {
temp = a;
a = c;
c = temp;
}
if (a > d) {
temp = a;
a = d;
d = temp;
}
if (b > c) {
temp = b;
b = c;
c = temp;
}
if (b > d) {
temp = b;
b = d;
d = temp;
}
if (c > d) {
temp = c;
c = d;
d = temp;
}
// 输出结果
printf("从小到大的顺序为:%d %d %d %d\n", a, b, c, d);
return 0;
}
```
这个程序中,我们使用了冒泡排序的思想,将输入的四个数从小到大排序,并输出结果。
阅读全文
相关推荐
![](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)