用C++写一个三个数找最大数
时间: 2023-03-19 10:21:47 浏览: 114
在 C 语言中,你可以使用 `if` 语句来找到三个数中的最大数。
例如,假设你想要找到三个数 `a`、`b` 和 `c` 中的最大数。你可以这样写:
```
if (a > b) {
if (a > c) {
printf("%d is the largest number.\n", a);
} else {
printf("%d is the largest number.\n", c);
}
} else {
if (b > c) {
printf("%d is the largest number.\n", b);
} else {
printf("%d is the largest number.\n", c);
}
}
```
这段代码会先比较 `a` 和 `b`,如果 `a` 比 `b` 大,就比较 `a` 和 `c`。如果 `a` 比 `c` 大,就输出 `a`;否则,输出 `c`。如果 `b` 比 `a` 大,就比较 `b` 和 `c`。如果 `b` 比 `c` 大,就输出 `b`;否则,输出 `c`。
这样就可以找到三个数中的最大数了。
相关问题
用C++写一个输出三个数中的最大数
以下是用C语言实现输出三个数中的最大数的代码:
```c
#include<stdio.h>
int main(){
int a, b, c, max;
printf("请输入三个整数:\n");
scanf("%d%d%d", &a, &b, &c);
max = a;//假设a为最大值
if (b > max) {//如果b比a大,则更新最大值为b
max = b;
}
if (c > max) {//如果c比当前最大值大,则更新最大值为c
max = c;
}
printf("最大值为:%d\n", max);
return 0;
}
```
首先,使用`scanf()`函数接收三个整数的输入。然后,我们假设第一个数为最大值。接下来,使用`if`语句进行比较,如果第二个数比当前最大值大,则更新最大值为第二个数;如果第三个数比当前最大值大,则更新最大值为第三个数。最后,输出最大值即可。
用c++写一个比较三个数的大小
以下是使用C语言编写的比较三个数大小的代码示例:
```
#include <stdio.h>
int main() {
int num1, num2, num3;
printf("请输入三个数字:\n");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 > num2 && num1 > num3) {
printf("%d 是最大的数字。\n", num1);
} else if (num2 > num1 && num2 > num3) {
printf("%d 是最大的数字。\n", num2);
} else {
printf("%d 是最大的数字。\n", num3);
}
return 0;
}
```
在这个示例中,我们使用了`if`和`else if`语句来比较三个数字的大小,并使用`printf`语句输出最大的数字。