写一个c语言程序,输入3个数字,按从小到大的顺序输出
时间: 2023-05-16 09:05:47 浏览: 66
当然可以,以下是代码:
```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;
}
```
希望能够帮到你!
相关问题
c语言写个程序,将三个数字从小到大排列
### 回答1:
下面是一个使用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;
}
```
上面的代码通过使用临时变量和if语句进行交换实现了从小到大排列。
### 回答2:
以下是使用C语言编写的将三个数字从小到大排列的程序:
```c
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void sort(int *arr) {
if (arr[0] > arr[1])
swap(&arr[0], &arr[1]);
if (arr[1] > arr[2]) {
swap(&arr[1], &arr[2]);
if (arr[0] > arr[1])
swap(&arr[0], &arr[1]);
}
}
int main() {
int arr[3];
printf("请输入三个数字:\n");
scanf("%d %d %d", &arr[0], &arr[1], &arr[2]);
sort(arr);
printf("从小到大排列为:\n");
printf("%d %d %d\n", arr[0], arr[1], arr[2]);
return 0;
}
```
程序中使用了两个函数,`swap`函数用来交换两个数字的值,`sort`函数用来将数组中的数字从小到大排列。在`main`函数中,先获取用户输入的三个数字,然后调用`sort`函数对数组进行排序,最后将排序后的结果输出到屏幕上。
编译并运行该程序后,用户可以输入三个数字,程序将会按照从小到大的顺序输出这三个数字。例如,用户输入的数字为`3 1 2`,则程序输出`1 2 3`。
### 回答3:
下面是一个使用C语言编写的程序,可以将输入的三个数字按从小到大的顺序进行排列。
```c
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void sort(int *a, int *b, int *c) {
if (*a > *b) {
swap(a, b);
}
if (*b > *c) {
swap(b, c);
}
if (*a > *b) {
swap(a, b);
}
}
int main() {
int num1, num2, num3;
printf("请输入三个数字:");
scanf("%d %d %d", &num1, &num2, &num3);
sort(&num1, &num2, &num3);
printf("从小到大排序后的结果为:%d %d %d\n", num1, num2, num3);
return 0;
}
```
程序中的`sort`函数用于进行排序,使用了交换元素的辅助函数`swap`。在`main`函数中,首先要求用户输入三个数字,然后将它们的地址传递给`sort`函数进行排序,最后输出排序后的结果。
程序的思想是通过两两比较和交换元素的方式,将最小的元素移到第一位,然后在剩余的两个元素中寻找较小的元素放在第二位,最后剩下最大的元素放在第三位。这样就能实现将三个数字从小到大排列。
c语言输入三个数字然后按顺序输出
好的,可以使用数组来存储这三个数字,然后通过冒泡排序算法将这三个数字按照从小到大的顺序排列,最后输出即可。下面是示例代码:
```c
#include <stdio.h>
int main() {
int nums[3];
int i, j, temp;
// 输入三个数字
for (i = 0; i < 3; i++) {
printf("请输入第%d个数字:", i+1);
scanf("%d", &nums[i]);
}
// 冒泡排序
for (i = 0; i < 2; i++) {
for (j = 0; j < 2-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 < 3; i++) {
printf("%d ", nums[i]);
}
return 0;
}
```
运行程序后,依次输入三个数字,程序会自动将它们按照从小到大的顺序输出。