请用c语言编写该程序:输入4个整数,编程用比较法由小到大顺序输出这4个数
时间: 2024-05-08 19:16:40 浏览: 9
```c
#include <stdio.h>
int main() {
int a, b, c, d, temp;
// 输入4个整数
printf("请输入4个整数:\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;
}
```
相关问题
用c语言编写一个程序:输入三个整数,按由小到大的顺序输出。
以下是用 C 语言编写的程序,可以实现输入三个整数,按由小到大的顺序输出:
```c
#include <stdio.h>
int main() {
int a, b, c;
printf("请输入三个整数:\n");
scanf("%d%d%d", &a, &b, &c);
if (a > b) {
int t = a;
a = b;
b = t;
}
if (a > c) {
int t = a;
a = c;
c = t;
}
if (b > c) {
int t = b;
b = c;
c = t;
}
printf("由小到大的顺序输出为:%d %d %d\n", a, b, c);
return 0;
}
```
希望能够帮到您!
用c语言编写一个程序使用冒泡排序:输入三个整数,按由小到大的顺序输出。
当然可以,以下是代码:
```
#include <stdio.h>
void bubble_sort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[3];
int i;
for (i = 0; i < 3; i++) {
scanf("%d", &arr[i]);
}
bubble_sort(arr, 3);
for (i = 0; i < 3; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
输入三个整数后,程序会按照由小到大的顺序输出这三个整数。