用c语言,输入2个整数,将其从小到大输出
时间: 2023-06-02 12:01:36 浏览: 239
以下是使用C语言编写的程序,输入两个整数,将它们从小到大输出:
```c
#include <stdio.h>
int main() {
int num1, num2, temp;
printf("请输入两个整数:");
scanf("%d %d", &num1, &num2);
if (num1 > num2) {
temp = num1;
num1 = num2;
num2 = temp;
}
printf("从小到大输出:\n%d\n%d\n", num1, num2);
return 0;
}
```
程序首先使用`scanf()`函数从用户输入中读取两个整数,并将它们存储在`num1`和`num2`变量中。然后,使用`if`语句比较这两个数的大小。如果`num1`大于`num2`,则交换它们的值。最后,使用`printf()`函数将这两个数从小到大输出。
相关问题
用C语言输入3个整数,从小到大输出
好的,以下是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语言输入10个整数,将其按从小到大排序并输出
好的,以下是C语言实现方法:
```c
#include <stdio.h>
int main() {
int nums[10];
int i, j, temp;
// 输入10个整数
printf("请输入10个整数:\n");
for (i = 0; i < 10; i++) {
scanf("%d", &nums[i]);
}
// 冒泡排序,从小到大排序
for (i = 0; i < 9; i++) {
for (j = 0; j < 9 - i; j++) {
if (nums[j] > nums[j+1]) {
temp = nums[j];
nums[j] = nums[j+1];
nums[j+1] = temp;
}
}
}
// 输出排序后的结果
printf("从小到大排序后的结果为:\n");
for (i = 0; i < 10; i++) {
printf("%d ", nums[i]);
}
printf("\n");
return 0;
}
```
输入10个整数后,使用冒泡排序算法进行从小到大排序,最后输出排序后的结果。
相关推荐
![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)