Find the minimum value among three given numbers.
To find the minimum of three numbers a, b, and c:
Numbers: a = 10, b = 5, c = 8 Step 1: Is a < b AND a < c? 10 < 5? NO → a is not minimum Step 2: Is b < a AND b < c? 5 < 10? YES AND 5 < 8? YES → b is minimum! Result: Minimum is 5
Accept three integers as input
Print the output as "Minimum is ___"
-10^15 <= INPUT <= 10^15
#include <iostream> #include <cmath> // Note: min() is actually in <algorithm>, not <cmath> using namespace std; int main() { long long a, b, c, minimum; cin >> a >> b >> c; // Method 1: Using built-in min() function (commented out) // minimum = min(min(a, b), c); // Requires <algorithm> // Method 2: Using if-else logic if (a < b && a < c) minimum = a; else if (b < a && b < c) minimum = b; else minimum = c; cout << "Minimum is " << minimum; return 0; }
The code includes <cmath>
, but the min()
function is actually defined in <algorithm>
. The code may still work on some compilers because <cmath>
sometimes transitively includes <algorithm>
, but this is compiler-dependent and not guaranteed by the C++ standard. For portable code, always include <algorithm>
when using min()
or max()
.
<
) to compare valuesmin()
function vs manual comparisonDecision Tree:
Is a < b AND a < c? ├── YES → minimum = a ✓ (DONE) └── NO → Continue... Is b < a AND b < c? ├── YES → minimum = b ✓ (DONE) └── NO → minimum = c ✓ (DONE) Logic: If neither a nor b is smallest, then c must be!
Why the Else Clause Works:
a
is not smaller than both others → it's not minimumb
is not smaller than both others → it's not minimumc
must be the minimum (or equal to minimum)C++ provides the min()
function from the <algorithm>
header:
#include <iostream> #include <algorithm> // Required for min() function using namespace std; int main() { long long a, b, c; cin >> a >> b >> c; // Nested min() - find min of (a,b), then compare with c long long minimum = min(min(a, b), c); cout << "Minimum is " << minimum; return 0; }
How it works:
min(a, b)
→ returns the smaller of a and bmin(result, c)
→ compares that result with cC++11 and later: You can also use initializer lists:
long long minimum = min({a, b, c}); // Requires C++11 or later
⚠️ Important Note:
The code includes <cmath>
instead of <algorithm>
, but it still works on many compilers because <cmath>
sometimes implicitly includes <algorithm>
. However, this is non-standard behavior! Always use <algorithm>
when you need min()
or max()
functions for portability.
Aspect | Manual If-Else Logic | Built-in min() Function |
---|---|---|
Code Length | Longer (7-8 lines) | Shorter (1 line) |
Readability | Clear logic flow | More concise |
Performance | Similar (compiler optimizes both) | Similar |
Learning Value | High (understand comparison logic) | Medium |
Flexibility | Easy to add custom logic | Limited customization |
Error Prone | More chances for logic errors | Less error-prone |
Best For | Learning, interviews, custom needs | Production code, quick solutions |
// ❌ WRONG: Only checks if a < b, not if a < c if (a < b) minimum = a; // What if c is smaller than a?
// ❌ WRONG: a must be smaller than BOTH b and c if (a < b || a < c) // Using OR is incorrect! minimum = a;
>
instead of <
for minimumint
for large values instead of long long
Approach 1: Simplified If-Else (Assuming a is minimum first)
long long minimum = a; // Assume a is minimum if (b < minimum) minimum = b; // Update if b is smaller if (c < minimum) minimum = c; // Update if c is smaller cout << "Minimum is " << minimum;
Approach 2: Ternary Operator (Nested)
long long minimum = (a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c); cout << "Minimum is " << minimum;
Approach 3: Using Array and Loop (Scalable)
long long arr[3] = {a, b, c}; long long minimum = arr[0]; for (int i = 1; i < 3; i++) { if (arr[i] < minimum) minimum = arr[i]; } cout << "Minimum is " << minimum;