Find Biggest of Three Numbers in C#

Beginner
⏱️ 9 min read
📚 Updated: May 2026
🎯 2 Code Examples
Conditionals

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 with static 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.

If-ladder explicit branches
Math.Max Max(a,b) then vs c
General N loop + running max

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.

Example

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.

14, 7, 22 Max 22
Check
22 ≥ 14 and 22 ≥ 7
Verdict
Matches the sample program output.
5, 5, 3 Max 5
Tie
Two values share the maximum.
Verdict
>= branches return 5 (first branch wins here).
9, 9, 9 Max 9
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).

Whole numbers match the C# samples; decimals still work in the preview.

Live result
Press “Run” to see the biggest value.

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.

📜 Pseudocode

Pseudocode
function findBiggest(a, b, c):
    if a >= b and a >= c:
        return a
    if b >= a and b >= c:
        return b
    return c
1

If–else if ladder (classic style)

Straightforward interview solution: three branches with && and >=, same logic as the original walkthrough.

c#
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 >= num3

Candidate 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.

2

Nested Math.Max

Same answer, compact C# style. Easy to connect mentally with “folding” over longer lists later.

c#
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

Use an if–else if chain comparing each candidate against both others, or nest Math.Max: Math.Max(Math.Max(a, b), c). Both return the largest value; using >= in the ladder keeps behavior correct when two or all three numbers tie.
With strict > only, equal largest values can fall through depending on branch order. Using >= on both comparisons picks a winner whenever the current candidate ties for largest.
Yes: Math.Max works for int, long, double, and several other numeric types. For many values in a collection you might use LINQ’s Enumerable.Max after storing them in an array or list.
Extend the idea: loop while remembering a running maximum, or fold nested Max calls. Loops scale better for large N.
Only a fixed number of comparisons for three slots: O(1) time and O(1) extra space.
Plain comparisons do not add numbers, so max-of-three on ints does not overflow by itself. Watch overflow if you later combine this with sums or products.

🔄 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, 22The biggest number is: 22
5, 5, 3The biggest number is: 5
-1, -4, -2The 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.

Ties

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.

Negatives

All values below zero

Still works: the biggest value is the least negative (closest to zero).

C# detail

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

ApproachTimeExtra space
If-ladder or nested Math.Max for three intsO(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 &&, or Math.Max(Math.Max(a,b),c).
  • Watch-outs: tie behavior with strict > only, and scaling to many numbers with a loop.
Did you know?

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful