用C语言写三个数比较大小并排序
时间: 2023-04-04 13:03:38 浏览: 215
可以使用以下代码来实现:
#include <stdio.h>
int main() {
int a, b, c;
printf("请输入三个整数:\n");
scanf("%d %d %d", &a, &b, &c);
if (a > b) {
int temp = a;
a = b;
b = temp;
}
if (a > c) {
int temp = a;
a = c;
c = temp;
}
if (b > c) {
int temp = b;
b = c;
c = temp;
}
printf("从小到大排序后的结果为:%d %d %d\n", a, b, c);
return 0;
}
相关问题
C语言比较三个数大小并从小到大输出
可以使用if语句来比较三个数的大小,并使用数组和循环语句来从小到大输出。以下是示例代码:
```
#include <stdio.h>
int main() {
int a, b, c;
printf("请输入三个整数:\n");
scanf("%d %d %d", &a, &b, &c);
// 比较三个数的大小
if (a > b) {
int temp = a;
a = b;
b = temp;
}
if (a > c) {
int temp = a;
a = c;
c = temp;
}
if (b > c) {
int temp = b;
b = c;
c = temp;
}
// 从小到大输出
int arr[3] = {a, b, c};
printf("从小到大排序为:\n");
for (int i = 0; i < 3; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
--相关问题--:
用c语言把三个数从大到小排序
好的,我可以为您提供一个用C语言编写的将三个数字从大到小排序的代码示例:
```
#include <stdio.h>
int main()
{
int a, b, c, temp;
printf("请输入三个整数:\n");
scanf("%d %d %d", &a, &b, &c);
if (a < b)
{
temp = a;
a = b;
b = temp;
}
if (a < c)
{
temp = a;
a = c;
c = temp;
}
if (b < c)
{
temp = b;
b = c;
c = temp;
}
printf("排序后的结果为:%d, %d, %d\n", a, b, c);
return 0;
}
```
这段代码通过比较三个整数的大小关系,交换它们的位置,从而实现了将三个数字从大到小排序的功能。希望这能够帮到您!
相关推荐
![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)