输入三个实数找出最大的数
时间: 2023-09-27 10:06:22 浏览: 48
好的,您可以使用以下代码实现:
```
#include <iostream>
using namespace std;
int main() {
double a, b, c;
cout << "请输入三个实数:" << endl;
cin >> a >> b >> c;
double max_num = a;
if (b > max_num) {
max_num = b;
}
if (c > max_num) {
max_num = c;
}
cout << "最大的数是:" << max_num << endl;
return 0;
}
```
在这个程序中,我们使用了三个变量 `a`、`b`、`c` 分别表示输入的三个实数。然后我们定义了一个变量 `max_num`,用于保存最大的数。接着我们使用 if 语句来判断 `b` 和 `c` 是否比 `max_num` 大,如果是就将 `max_num` 更新为更大的数,最后输出 `max_num` 即可。
相关问题
C语言输入三个实数并找出最大的数
可以使用三目运算符来比较三个数的大小,以下是示例代码:
```
#include <stdio.h>
int main() {
float num1, num2, num3, max;
printf("请输入三个实数:\n");
scanf("%f %f %f", &num1, &num2, &num3);
max = (num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2 : num3);
printf("最大的数是 %.2f\n", max);
return 0;
}
```
首先通过 `scanf` 函数输入三个实数,然后使用三目运算符比较三个数的大小,并将最大的数赋值给变量 `max`。最后输出最大的数。注意,这里使用了 `%.2f` 格式化字符串来保留两位小数。
编写程序,从键盘上输入3个实数,输出最大数
以下是Python程序实现:
```python
a = float(input("请输入第一个实数:"))
b = float(input("请输入第二个实数:"))
c = float(input("请输入第三个实数:"))
if a > b and a > c:
print("最大数为:", a)
elif b > a and b > c:
print("最大数为:", b)
else:
print("最大数为:", c)
```
程序首先从键盘上输入3个实数,使用`float()`函数将输入的字符串转换为浮点数类型。然后使用`if`语句判断最大数是哪一个,并输出结果。如果第一个数`a`大于另外两个数`b`和`c`,则`a`为最大数;如果第二个数`b`大于第一个数`a`和第三个数`c`,则`b`为最大数;否则,第三个数`c`为最大数。
相关推荐
![text/x-c++; charset=iso-8859-1](https://img-home.csdnimg.cn/images/20210720083646.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)