- Check
22 ≥ 14and22 ≥ 7- Verdict
- Matches the sample program output.
Find Biggest of Three Numbers in C#
What you’ll learn
- How to pick the largest of three integers with a clear if–else if–else ladder.
- An equivalent one-liner style using nested
Math.Max(common in C#). - Why
>=matters when values can tie, plus a browser live preview.
Overview
Given three integers, return the one that is not smaller than the other two. When two or three values tie for largest, any of the tied values counts as correct; the ladder below returns one of them in a fixed order.
Two programs
Classic if-ladder (same idea as the original tutorial) plus Math.Max nesting.
Live preview
Type three integers and see the maximum immediately.
Interview polish
Mention ties, scaling to N values, and O(1) cost for exactly three numbers.
Prerequisites
Comfort with if, else if, else, and relational operators >, >=.
using System;, a console program withstatic void Main(), and integer literals.- Logical
&&to combine two comparisons in one condition.
What does “biggest of three” mean?
For integers a, b, and c, the maximum (biggest) is any value m in {a,b,c} such that m ≥ a, m ≥ b, and m ≥ c. If the largest value appears more than once, those inequalities still hold.
The classic program uses >= on both sides so ties land in the first matching branch without falling through incorrectly.
Formal note
Binary max is associative on totally ordered numbers: max(a, b, c) = max(max(a, b), c). That is exactly what nested Math.Max expresses.
max(14, 7, 22) = 22 because 22 is greater than or equal to both 14 and 7.
Intuition and examples
Imagine three poles of different heights: you only need to remember which one is tallest. With exactly three slots, you can spell out those comparisons in code instead of sorting.
- Tie
- Two values share the maximum.
- Verdict
>=branches return 5 (first branch wins here).
- Triple tie
- Every value is largest.
- Verdict
- First branch returns 9.
Takeaway: you are comparing sizes, not sorting the whole list.
Live preview
Enter three integers. The widget uses JavaScript Math.max on three numbers (ties behave like the built-in maximum).
Algorithm
Goal: return the largest of three comparable values a, b, c.
Test whether a wins
If a ≥ b and a ≥ c, then a is a maximum; return it.
Else test whether b wins
If b ≥ a and b ≥ c, return b.
Otherwise return c
If neither a nor b beats both rivals, c must.
Alternative: nest Math.Max
Math.Max(Math.Max(a, b), c) applies the same associative idea with library helpers.
📜 Pseudocode
function findBiggest(a, b, c):
if a >= b and a >= c:
return a
if b >= a and b >= c:
return b
return c If–else if ladder (classic style)
Straightforward interview solution: three branches with && and >=, same logic as the original walkthrough.
using System;
class Program
{
static int FindBiggest(int num1, int num2, int num3)
{
if (num1 >= num2 && num1 >= num3)
{
return num1;
}
else if (num2 >= num1 && num2 >= num3)
{
return num2;
}
else
{
return num3;
}
}
static void Main()
{
int number1 = 14;
int number2 = 7;
int number3 = 22;
int result = FindBiggest(number1, number2, number3);
Console.WriteLine($"The biggest number is: {result}");
}
} Explanation
Each branch names one candidate and checks it against both others.
num1 >= num2 && num1 >= num3Candidate num1. Both comparisons must succeed so num1 ties or beats both rivals.
if (num2 >= num1 && num2 >= num3)Only runs if the first test failed. Same pattern for num2.
return num3;Else branch. If num1 and num2 are not maximal, num3 is.
Nested Math.Max
Same answer, compact C# style. Easy to connect mentally with “folding” over longer lists later.
using System;
class Program
{
static int FindBiggest3(int a, int b, int c)
{
return Math.Max(Math.Max(a, b), c);
}
static void Main()
{
int number1 = 14;
int number2 = 7;
int number3 = 22;
int result = FindBiggest3(number1, number2, number3);
Console.WriteLine($"The biggest number is: {result}");
}
} Explanation
Math.Max picks the larger of two values; ties return either operand, which is fine because any tied largest is acceptable.
Math.Max(Math.Max(a, b), c)Fold. Reduce the first pair, then compare the winner with c.
Optimization
Three values only. Every correct solution is already O(1); tiny micro-optimizations rarely matter.
Larger N. Read values into an array or list and track best in one pass instead of writing giant comparison chains.
Floating-point. Math.Max has overloads for double and float as well.
Interview: write the if-ladder first for clarity; mention Math.Max as a tidy refactor.
❓ FAQ
🔄 Input / output examples
Both samples print one line. Change the three literals, or read from the console with something like int.Parse / int.TryParse on split tokens.
Inputs (a, b, c) | Printed line |
|---|---|
14, 7, 22 | The biggest number is: 22 |
5, 5, 3 | The biggest number is: 5 |
-1, -4, -2 | The biggest number is: -1 |
Edge cases and pitfalls
Branch order matters if you mix strict > with uneven logic; be explicit when the problem needs a special tie-break.
Two or three equal maxima
The >= ladder returns the first candidate that can tie-win. That is usually acceptable; if the spec wants the smallest index among ties, adjust.
All values below zero
Still works: the biggest value is the least negative (closest to zero).
Math.Max with NaN (advanced)
For floating types, IEEE rules apply if NaN appears. Integer Math.Max does not involve NaN.
⏱️ Time and space complexity
| Approach | Time | Extra space |
|---|---|---|
If-ladder or nested Math.Max for three ints | O(1) | O(1) |
Max of N numbers (single pass) | O(N) | O(1) |
Both programs use only a few integer locals: auxiliary space stays constant.
Summary
- Task: return the maximum of three integers (ties allowed).
- Patterns: if-ladder with
>=and&&, orMath.Max(Math.Max(a,b),c). - Watch-outs: tie behavior with strict
>only, and scaling to many numbers with a loop.
Comparing three values takes only a few pairwise checks in the usual if-ladder form. You can also nest Math.Max twice—both styles are O(1) time and O(1) extra memory.
9 people found this page helpful
