用C语言输入3个整数,从小到大输出
时间: 2023-08-04 19:17:55 浏览: 42
好的,以下是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;
}
```
首先,我们使用 `scanf` 函数从标准输入中读入三个整数。然后,使用三个 `if` 语句对这三个数进行排序,保证最终输出的是从小到大排序的结果。最后,使用 `printf` 函数输出排序后的三个整数。
相关问题
用c语言输入三个整数,从小到大输出
Sure, here's code for sorting three integers in C:
```
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three integers: ");
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("Sorted integers: %d %d %d", a, b, c);
return 0;
}
```
c语言输入三个整数从小到大排序
好的,以下是一个简单的C语言代码示例,可以实现输入三个整数,然后从小到大进行排序:
```c
#include <stdio.h>
int main() {
int a, b, c, temp;
printf("请输入三个整数:");
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;
}
```
在这个代码中,我们首先定义了三个整数变量:a、b、c,以及一个临时变量temp。然后,通过`scanf`语句,从用户输入中获取这三个整数的值。
接下来,我们使用`if`语句和临时变量temp来对这三个整数进行排序。`if`语句用于比较三个数的大小关系,临时变量temp用于交换两个变量的值。最后,我们使用`printf`语句将排序后的结果输出到屏幕上。
需要注意的是,C语言中没有提供像C++中的`swap`函数,因此我们需要使用临时变量来实现变量值的交换操作。