Given 2 numbers, find the maximum of 2 numbers
The >
operator checks if the left value is strictly greater than the right value.
Syntax: value1 > value2
Returns: true
if value1 is greater than value2, otherwise false
Examples:
123 > 98
→ TRUE (123 is greater than 98)98 > 123
→ FALSE (98 is not greater than 123)50 > 50
→ FALSE (50 is NOT greater than 50, they're equal)0 > -5
→ TRUE (0 is greater than -5)✅ Key point: The value must be strictly larger, not equal!
Opposite of less than:
Remember: >
is the opposite of <
a < b
→ a
is smallera > b
→ a
is largerUsing if-else to find maximum:
if (a > b)
max = a;
else
max = b;
Logic breakdown:
a
greater than b
?a
is the larger number, store it in max
b
is larger or equal, store it in max
Why this works:
If a > b
is false, it means either:
a < b
(b is larger) → max = b
✅a == b
(they're equal) → max = b
✅ (either works!)💡 Pattern recognition: This is the exact opposite of finding minimum!
Minimum: if (a < b) min = a; else min = b;
Maximum: if (a > b) max = a; else max = b;
The shortcut approach:
#include <cmath>
cout << "Maximum is " << max(a, b);
How max() works:
The max(a, b)
function compares two values and returns the larger one.
max(98, 123)
→ Returns 123
max(0, 68)
→ Returns 68
max(50, 50)
→ Returns 50
max(-10, -5)
→ Returns -5
(less negative)One-liner alternative solution:
cout << "Maximum of " << a << " and " << b << " is " << max(a, b);
✅ Pros: Clean, concise, impossible to mess up the logic
📚 Learning value: Understanding the manual approach helps you appreciate how max()
works internally
Both min()
and max()
are part of the <cmath>
library and work identically:
Comparison:
min(a, b)
→ Returns the smaller valuemax(a, b)
→ Returns the larger valueExample with a=10, b=20:
min(10, 20)
→ 10
max(10, 20)
→ 20
Manual equivalents:
// min logic
if (a < b) result = a; else result = b;
// max logic
if (a > b) result = a; else result = b;
💡 Pro tip: You can also chain them!
max(max(a, b), c)
→ Finds maximum of three numbers!
min(min(a, b), c)
→ Finds minimum of three numbers!
Example 1: a=98, b=123
long long a, b, max;
a = 98, b = 123
if (98 > 123)
→ FALSEmax = b;
→ max = 123
"Maximum of 98 and 123 is 123"
Example 2: a=0, b=68
long long a, b, max;
a = 0, b = 68
if (0 > 68)
→ FALSEmax = b;
→ max = 68
"Maximum of 0 and 68 is 68"
Example 3: a=200, b=50
long long a, b, max;
a = 200, b = 50
if (200 > 50)
→ TRUEmax = a;
→ max = 200
"Maximum of 200 and 50 is 200"
Edge case: a=100, b=100
if (100 > 100)
→ FALSE (equal, not greater)max = b;
→ max = 100
✅Notice the helpful comment in our code:
// you could either use max(a,b) from cmath library or
Why this comment is valuable:
Good commenting practices:
// increment i
💡 Pro tip: In interviews, commenting your code shows clear thinking and consideration of alternatives!
Where you'd use maximum finding:
More complex scenarios:
💡 This simple concept is the building block for sorting algorithms, data structures, and optimization problems!
💡 Tip: Master both min and max logic - they're mirror images of each other!
#include <iostream>
#include <cmath>
using namespace std;
int main() {
long long a, b, max;
cin >> a >> b;
// you could either use max(a,b) from cmath library or
if (a > b)
max = a;
else
max = b;
cout << "Maximum of " << a << " and " << b << " is " << max;
return 0;
}