C++程序 – 求三个数字中的最大值
在这里,我们将看到如何使用c++程序找到三个数字中最大的那个。下面是一些例子:
Input: a = 1, b = 2, c = 45Output: The Largest Among 3 is 45Input: a = 75, b = 134, c = 9Output: The Largest Among 3 is 134
在c++中有三种方法可以找到这三个数字中最大的那个:
- 使用if – else语句。使用逻辑运算符。使用三元运算符。
让我们开始详细讨论每一种方法。
1. 使用if – else语句
这里将使用以下算法:
算法:
- 开始.输入a, b and c.检查条件a>=b如果步骤3为“True”,请执行步骤5,否则请执行步骤9。检查条件a>=c。如果步骤5为“True”请执行步骤7,否则请执行步骤8。输入“The biggest Among 3 is: a”,执行步骤13输入“The biggest Among 3 is: c”,执行步骤13。检查条件b>=c。如果步骤9为“True”,执行步骤11,否则执行步骤12。输入“The biggest Among 3 is: b”,执行步骤13。输入“The biggest Among 3 is: c”,执行步骤13。结束.
流程图:

下面是使用if-else语句查找三个数字中最大的一个的c++程序:
// C++ Program to Find Largest Among// Three Numbers Using if-else// Statement#include<bits/stdc++.h>using namespace std; // Driver codeint main(){ int a, b, c; cout << "Enter the three numbers a, b & c" << endl; cin >> a >> b >> c; if(a >= b) { if(a >= c) { cout << "The Largest Among Three Numbers is : " << a << endl; } else { cout << "The Largest Among Three Numbers is : " << c << endl; } } else { if(b >= c) { cout << "The Largest Among Three Numbers is : " << b << endl; } else { cout << "The Largest Among Three Numbers is : " << c << endl; } } return 0;}输出:

2. 使用逻辑操作符
下面是使用逻辑运算符查找最大数字的流程图:

下面是使用逻辑运算符查找三个数字中最大的一个的c++程序:
// C++ program to find largest// among three numbers using // logical operator#include<bits/stdc++.h>using namespace std; // Driver codeint main(){ int a, b, c; cout << "Enter the three numbers a, b & c" << endl; cin >> a >> b >> c; if(a >= b && a >= c) { cout << "The Largest Among Three Numbers is : " << a << endl; } else if(b >= a && b >= c) { cout << "The Largest Among Three Numbers is : " << b << endl; } else { cout << "The Largest Among Three Numbers is : " << c << endl; } return 0;}输出:

3.使用三元运算符
下面是使用三元操作符查找三个数中最大的那个的c++程序:
// C++ program to find largest// among three numbers using // ternary operator#include<bits/stdc++.h>using namespace std; // Driver codeint main(){ int a, b, c, ans; cout << "Enter the three numbers a, b & c" << endl; cin >> a >> b >> c; ans = (a >= b ? (a >= c ? a : c) : (b >= c ? b : c)); cout << "The Largest Among Three Numbers is : " << ans << endl; return 0;}输出:

