用c语言完成 1.输入三个整数,按照由小到大的顺序输出这三个数。
时间: 2024-02-20 15:57:00 浏览: 36
好的,以下是用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;
}
```
运行程序后,会提示用户输入三个整数,程序会对这三个数进行比较,然后按照由小到大的顺序输出这三个数。程序中使用了三次if条件语句,通过比较大小来交换位置,从而实现从小到大的排序。
相关问题
c语言输入三个整数按由小到大的顺序输出用指针
以下是一个用指针实现的C语言程序,用于输入三个整数并按照由小到大的顺序输出:
```c
#include<stdio.h>
void sort(int *q1, int *q2, int *q3);
void swap(int *p4, int *p5);
int main() {
int a, b, c;
int *p1, *p2, *p3;
scanf("%d%d%d", &a, &b, &c);
p1 = &a;
p2 = &b;
p3 = &c;
sort(p1, p2, p3);
printf("%d %d %d", a, b, c);
return 0;
}
void sort(int *q1, int *q2, int *q3) {
void swap(int *p4, int *p5);
if (*q1 < *q2)
swap(q1, q2);
if (*q1 < *q3)
swap(q1, q3);
if (*q2 < *q3)
swap(q2, q3);
}
void swap(int *p4, int *p5) {
int t;
t = *p4;
*p4 = *p5;
*p5 = t;
}
```
这个程序首先定义了两个函数`sort`和`swap`,分别用于排序和交换两个整数的值。然后在`main`函数中,通过指针将输入的三个整数与变量`a`、`b`、`c`关联起来。接下来,调用`sort`函数对这三个整数进行排序。最后,使用`printf`函数按照由小到大的顺序输出这三个整数。
c语言编写程序输入三个整数要求按照由小到大的顺序输出
好的,您可以使用以下代码实现:
```c
#include <stdio.h>
int main() {
int num1, num2, num3;
int temp;
printf("请输入三个整数:\n");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 > num2) {
temp = num1;
num1 = num2;
num2 = temp;
}
if (num1 > num3) {
temp = num1;
num1 = num3;
num3 = temp;
}
if (num2 > num3) {
temp = num2;
num2 = num3;
num3 = temp;
}
printf("按照由小到大的顺序输出:%d %d %d\n", num1, num2, num3);
return 0;
}
```
在这段代码中,您首先要求用户输入三个整数,然后使用 if 语句进行比较和交换,最后按照从小到大的顺序进行输出。