Beginner · if/else + edge cases
Maximum of Three
Read three integers and print the largest. Sounds trivial — the interesting part is the edge cases: all-negative input, and ties.
Problem
Input: one line with three integers a b c (−10⁹ ≤ each ≤ 10⁹).
Output: a single line with the maximum of the three.
Sample:
| Input | Output |
|---|---|
3 9 5 | 9 |
-7 -2 -9 | -2 |
4 4 4 | 4 |
Your solution
Custom input for "Run my input"
Judging runs entirely in your browser. Hidden tests include the all-negative and all-equal cases.
Stuck? Hints
Hint 1 — a tempting wrong start
Initializing int max = 0; fails the all-negative test: the true answer −2 is smaller than 0. Initialize from the data (max = a;), never from a magic number.
Hint 2 — the running-maximum pattern
Start with max = a;, then if (b > max) max = b; and if (c > max) max = c;. This extends to a million values — that's why it's the pattern worth internalizing.
Solution (try first!)
string[] parts = Console.ReadLine().Split(' ');
int a = int.Parse(parts[0]);
int b = int.Parse(parts[1]);
int c = int.Parse(parts[2]);
int max = a;
if (b > max) max = b;
if (c > max) max = c;
Console.WriteLine(max);
// the one-liner: Math.Max nests, ties are naturally fine
string[] parts = Console.ReadLine().Split(' ');
int max = Math.Max(int.Parse(parts[0]), Math.Max(int.Parse(parts[1]), int.Parse(parts[2])));
Console.WriteLine(max);
Ties need no special code: > (not >=) keeps the earlier value, which equals the later one anyway. Next: Count the Digits.