Given three numbers a, b, and c, find the largest value — the one that is greater than or equal to both of the others. Ties are allowed: any tied maximum is a valid answer.
Remember
biggest = max(a, b, c)
If a ≥ b and a ≥ c → a
Else if b ≥ a and b ≥ c → b
Else → c
Example: 14, 22, 7 → biggest is 22
a
b
c
Biggest
14
22
7
22
-1
-4
-2
-1
5
5
3
5 (tie)
9
9
9
9
Approach
How to Find the Biggest in C#
Two solid options — show the ladder in interviews, use Math.Max in apps.
Read three numbers a, b, c.
Compare with >= (handles ties).
Return the winner — or nest Math.Max.
Test negatives and equal values.
Method
Idea
Best for
If-else ladder
a >= b && a >= c, then check b, else c
Interviews (shows logic)
Nested Math.Max
Math.Max(Math.Max(a, b), c)
Short application code
Custom Max2
Helper that returns the larger of two, then nest
Teaching without library calls
Pseudocode
Pseudocode
function Biggest(a, b, c):
if a >= b and a >= c:
return a
else if b >= a and b >= c:
return b
else:
return c
Cheat sheet
Goal
Pattern
a wins
a >= b && a >= c
b wins
b >= a && b >= c
Library shortcut
Math.Max(Math.Max(a, b), c)
Ties
Prefer >= so equal maxima still return
Try it
Live Preview
Enter three numbers to see each comparison and the biggest value.
Integers or decimals. Ties return one of the equal largest values.
Result
a = 14, b = 22, c = 7
a >= b && a >= c? false
b >= a && b >= c? true
Biggest = 22
Trace
Worked Walkthrough — 14, 22, 7
Apply the if-else ladder with >=.
Check
Result
14 >= 22 && 14 >= 7
false (a is not biggest)
22 >= 14 && 22 >= 7
true → return 22
No need to test c once b wins. Same result as Math.Max(Math.Max(14, 22), 7).
Code
C# Programs
Three programs: if-else ladder, nested Math.Max, and a custom Max2 helper. Use View Output for sample results.
Example 1 — If-Else Ladder
Interview default: compare with >= and return the winner.
C#
using System;
class Program
{
static int Biggest(int a, int b, int c)
{
if (a >= b && a >= c)
{
return a;
}
else if (b >= a && b >= c)
{
return b;
}
else
{
return c;
}
}
static void Main()
{
int a = 14, b = 22, c = 7;
Console.WriteLine("The biggest number is: " + Biggest(a, b, c));
}
}
Output
The biggest number is: 22
How It Works
1. Test a. If a is at least as large as b and c, return a.
2. Test b. Otherwise, if b beats both, return b.
3. Else c. The remaining case is that c is the biggest (or tied and reached last).
Example 2 — Nested Math.Max
Math.Max takes two arguments, so nest a call for three values.
C#
using System;
class Program
{
static int Biggest(int a, int b, int c)
{
return Math.Max(Math.Max(a, b), c);
}
static void Main()
{
Console.Write("Enter three integers (a b c): ");
string[] parts = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
if (parts.Length != 3
|| !int.TryParse(parts[0], out int a)
|| !int.TryParse(parts[1], out int b)
|| !int.TryParse(parts[2], out int c))
{
Console.WriteLine("Please enter three integers.");
return;
}
Console.WriteLine("The biggest number is: " + Biggest(a, b, c));
}
}
Output (when user enters 14 22 7)
Enter three integers (a b c): 14 22 7
The biggest number is: 22
How It Works
1. Pairwise max.Math.Max(a, b) picks the larger of the first two.
2. Fold in c.Math.Max(that, c) finishes the three-way maximum.
3. Same result. Equivalent to the if-else ladder — shorter once the logic is clear.
Example 3 — Custom Max2 Helper
Teach the idea without calling the library — nest your own two-argument max.
C#
using System;
class Program
{
static int Max2(int x, int y)
{
return x >= y ? x : y;
}
static int Biggest(int a, int b, int c)
{
return Max2(Max2(a, b), c);
}
static void Main()
{
Console.WriteLine(Biggest(14, 22, 7)); // 22
Console.WriteLine(Biggest(-1, -4, -2)); // -1
Console.WriteLine(Biggest(5, 5, 3)); // 5
}
}
Output
22
-1
5
How It Works
1. Max of two.Max2 returns the larger of a pair using >=.
2. Nest for three.Max2(Max2(a, b), c) mirrors nested Math.Max.
3. Cover edge cases. Negatives and ties behave correctly — assert them in interviews.
Edge Cases & Pitfalls
Check these before calling the solution done.
> vs >=
Prefer >=
Strict > can skip equal largest values depending on branch order.
Ties
Any max is fine
If two or three share the maximum, returning one of them is correct.
Negatives
Least negative wins
max(-1, -4, -2) is -1, not the largest magnitude.
Sort?
Unnecessary
Sorting three values works but is overkill — prefer comparisons.
Interview
Ladder first
Write if-else, then mention Math.Max as a shortcut.
Many values
Use a loop
For N > 3, keep a running maximum instead of deeper nesting.
Analysis
Time and Space Complexity
Program
Time
Extra space
If-else ladder
O(1)
O(1)
Nested Math.Max
O(1)
O(1)
Running max over N values
O(N)
O(1)
Remember
Key Takeaways
Goal: return the largest among a, b, and c.
Use >=: ties still return a valid maximum.
Shortcut:Math.Max(Math.Max(a, b), c).
Test set: 14,22,7 → 22; -1,-4,-2 → -1; 5,5,3 → 5.
One line: compare with a few >= checks (or nest Math.Max) and return the largest.
Frequently Asked Questions
Compare a, b, and c with if-else (using >=), or nest Math.Max: Math.Max(Math.Max(a, b), c). Both return the largest value.
>= handles ties cleanly. If two values share the maximum, one of them is still returned correctly.
Yes. Math.Max takes two arguments, so nest it for three: Math.Max(Math.Max(a, b), c). In interviews, show the if-else version first.
Any of them is a valid answer — they all share the same maximum.
Yes. Among negatives, the largest is the least negative (for example max(-1, -4, -2) is -1).
For exactly three numbers, time is O(1) and extra space is O(1).
Use a loop with a running maximum, or fold Math.Max across an array.
Yes. It is equivalent to Math.Max(Math.Max(a, b), c) and still O(1).
You can, but sorting is unnecessary overhead for a fixed count of three. Prefer comparisons or Math.Max.
The same comparison logic works for double. Be careful with NaN if you accept user floats.
🤔
Did you know?
With only three values you need a handful of comparisons, so the check is O(1) time and O(1) extra space.