c语言数字排序由小到大
时间: 2024-01-26 20:14:02 浏览: 25
以下是使用冒泡排序算法实现C语言数字排序由小到大的示例代码:
```c
#include <stdio.h>
int main() {
int nums[] = {5, 2, 8, 3, 1, 7, 9, 4, 6}; // 待排序的数字数组
int len = sizeof(nums) / sizeof(int); // 数组长度
// 冒泡排序
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - 1 - i; j++) {
if (nums[j] > nums[j + 1]) {
int temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
// 输出排序结果
printf("排序后的结果为:\n");
for (int i = 0; i < len; i++) {
printf("%d ", nums[i]);
}
return 0;
}
```
输出结果为:
```
排序后的结果为:
1 2 3 4 5 6 7 8 9
```
相关问题
c语言4个数字大小排序
以下是使用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;
}
```
首先,我们通过 `scanf` 函数从用户输入中获取四个整数。然后,我们使用多个 `if` 语句来比较四个数字的大小,并将它们按升序排列。最后,我们使用 `printf` 函数输出排序后的结果。
c语言3个数字大小排序
以下是使用 C 语言实现的三个数字大小排序的代码示例:
```c
#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;
}
```
该程序中,使用 `scanf` 函数获取用户输入的三个数字,依次比较它们的大小,然后将它们按从小到大的顺序输出。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)