c语言三个数怎么编程

时间:2025-01-28 21:46:48 网络游戏

求和程序

```c

include

int main() {

int a, b, c;

int sum;

printf("请输入三个整数:");

scanf("%d %d %d", &a, &b, &c);

sum = a + b + c;

printf("三个数的和是:%d\n", sum);

return 0;

}

```

求和及平均值程序

```c

include

int main() {

int a, b, c;

float sum, average;

printf("请输入三个整数:");

scanf("%d %d %d", &a, &b, &c);

sum = a + b + c;

average = sum / 3.0;

printf("三个数的和是:%d\n", sum);

printf("三个数的平均值是:%.2f\n", average);

return 0;

}

```

排序及求和程序

```c

include

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

}

int main() {

int a, b, c;

printf("请输入三个整数:");

scanf("%d %d %d", &a, &b, &c);

if (a > b) swap(&a, &b);

if (a > c) swap(&a, &c);

if (b > c) swap(&b, &c);

printf("三个数从小到大排序为:%d %d %d\n", a, b, c);

return 0;

}

```

找出最大值程序

```c

include

int findMax(int a, int b, int c) {

int max = a;

if (b > max) max = b;

if (c > max) max = c;

return max;

}

int main() {

int a, b, c;

printf("请输入三个整数:");

scanf("%d %d %d", &a, &b, &c);

int max = findMax(a, b, c);

printf("三个数中的最大值是:%d\n", max);

return 0;

}

```

输入三个数并按从小到大排序输出

```c

include

int main() {

int a, b, c;

printf("请输入三个整数:");

scanf("%d %d %d", &a, &b, &c);

if (a > b) swap(&a, &b);

if (a > c) swap(&a, &c);

if (b > c) swap(&b, &c);

printf("三个数从小到大排序为:%d %d %d\n", a, b, c);

return 0;

}

```

这些程序涵盖了从简单的求和到找出最大值以及排序等多个方面的需求。你可以根据具体的应用场景选择合适的程序进行参考和修改。