0
0
CsharpProgramBeginner · 2 min read

C# Program to Find Largest of Three Numbers

In C#, you can find the largest of three numbers using if-else statements like this: if (a >= b && a >= c) largest = a; else if (b >= a && b >= c) largest = b; else largest = c;.
📋

Examples

Inputa=3, b=7, c=5
Output7
Inputa=10, b=10, c=2
Output10
Inputa=-1, b=-5, c=-3
Output-1
🧠

How to Think About It

To find the largest of three numbers, compare the first number with the other two using && to check if it is greater or equal to both. If true, it is the largest. Otherwise, check the second number similarly. If neither the first nor second is largest, the third must be the largest.
📐

Algorithm

1
Get the three numbers as input.
2
Check if the first number is greater than or equal to both the second and third numbers.
3
If yes, the first number is the largest.
4
Otherwise, check if the second number is greater than or equal to both the first and third numbers.
5
If yes, the second number is the largest.
6
Otherwise, the third number is the largest.
7
Print the largest number.
💻

Code

csharp
using System;
class Program {
    static void Main() {
        int a = 3, b = 7, c = 5;
        int largest;
        if (a >= b && a >= c) {
            largest = a;
        } else if (b >= a && b >= c) {
            largest = b;
        } else {
            largest = c;
        }
        Console.WriteLine(largest);
    }
}
Output
7
🔍

Dry Run

Let's trace the example where a=3, b=7, c=5 through the code.

1

Compare a with b and c

Check if 3 >= 7 and 3 >= 5 (false)

2

Compare b with a and c

Check if 7 >= 3 and 7 >= 5 (true)

3

Assign largest

largest = 7

4

Print largest

Output: 7

StepConditionResultLargest
13 >= 7 && 3 >= 5falseN/A
27 >= 3 && 7 >= 5true7
3Assign largest-7
4Print largest-7
💡

Why This Works

Step 1: Compare first number

We use if (a >= b && a >= c) to check if the first number is greater or equal to both others.

Step 2: Compare second number

If the first is not largest, check if the second number is greater or equal to both others with else if (b >= a && b >= c).

Step 3: Assign third number

If neither first nor second is largest, the third number must be largest, so assign it.

Step 4: Print result

Finally, print the largest number to show the result.

🔄

Alternative Approaches

Using Math.Max
csharp
using System;
class Program {
    static void Main() {
        int a = 3, b = 7, c = 5;
        int largest = Math.Max(a, Math.Max(b, c));
        Console.WriteLine(largest);
    }
}
This approach is shorter and uses built-in functions but may be less clear for beginners learning comparisons.
Using ternary operator
csharp
using System;
class Program {
    static void Main() {
        int a = 3, b = 7, c = 5;
        int largest = (a >= b && a >= c) ? a : (b >= c ? b : c);
        Console.WriteLine(largest);
    }
}
This uses a compact conditional expression but can be harder to read for new learners.

Complexity: O(1) time, O(1) space

Time Complexity

The program performs a fixed number of comparisons regardless of input size, so it runs in constant time O(1).

Space Complexity

Only a few variables are used to store inputs and the largest number, so space complexity is O(1).

Which Approach is Fastest?

All approaches run in constant time; using Math.Max is concise but function calls may have slight overhead compared to direct comparisons.

ApproachTimeSpaceBest For
If-else comparisonsO(1)O(1)Clear logic for beginners
Math.Max functionO(1)O(1)Concise and clean code
Ternary operatorO(1)O(1)Compact code, less readable for beginners
💡
Use Math.Max for a quick and clean way to find the largest number.
⚠️
Beginners often forget to use && to compare the first number with both others, causing wrong results.