0
0
CProgramBeginner · 2 min read

C Program to Find Largest of Three Numbers

To find the largest of three numbers in C, use if-else statements to compare the numbers like if (a > b && a > c) largest = a; and print the largest value.
📋

Examples

Inputa=5, b=3, c=9
OutputLargest number is 9
Inputa=10, b=10, c=5
OutputLargest number is 10
Inputa=-1, b=-5, c=-3
OutputLargest number is -1
🧠

How to Think About It

To find the largest of three numbers, compare the first number with the second and third using && to check if it is greater than both. If true, it is the largest. Otherwise, compare the second number with the third. The one that passes the comparison is the largest.
📐

Algorithm

1
Get input values for three numbers a, b, and c
2
Check if a is greater than both b and c
3
If yes, set largest to a
4
Else check if b is greater than c
5
If yes, set largest to b
6
Otherwise, set largest to c
7
Print the largest number
💻

Code

c
#include <stdio.h>

int main() {
    int a, b, c, largest;
    printf("Enter three numbers: ");
    scanf("%d %d %d", &a, &b, &c);

    if (a >= b && a >= c) {
        largest = a;
    } else if (b >= c) {
        largest = b;
    } else {
        largest = c;
    }

    printf("Largest number is %d\n", largest);
    return 0;
}
Output
Enter three numbers: 5 3 9 Largest number is 9
🔍

Dry Run

Let's trace the input a=5, b=3, c=9 through the code

1

Input values

a=5, b=3, c=9

2

Check if a >= b and a >= c

5 >= 3 is true, 5 >= 9 is false, so condition is false

3

Check if b >= c

3 >= 9 is false

4

Set largest to c

largest = 9

5

Print largest

Output: Largest number is 9

StepConditionResultLargest
1a >= b && a >= cfalse
2b >= cfalse
3Set largest = c9
💡

Why This Works

Step 1: Compare first number

The code uses if (a >= b && a >= c) to check if the first number is greater than or equal to both others.

Step 2: Compare second number

If the first number is not largest, it checks if the second number is greater than or equal to the third using else if (b >= c).

Step 3: Assign largest

If neither first nor second is largest, the third number is assigned as largest by default.

🔄

Alternative Approaches

Using nested if-else
c
#include <stdio.h>

int main() {
    int a, b, c, largest;
    printf("Enter three numbers: ");
    scanf("%d %d %d", &a, &b, &c);

    if (a >= b) {
        if (a >= c) {
            largest = a;
        } else {
            largest = c;
        }
    } else {
        if (b >= c) {
            largest = b;
        } else {
            largest = c;
        }
    }

    printf("Largest number is %d\n", largest);
    return 0;
}
This approach uses nested if-else to reduce the number of comparisons in some cases but is slightly more complex to read.
Using ternary operator
c
#include <stdio.h>

int main() {
    int a, b, c, largest;
    printf("Enter three numbers: ");
    scanf("%d %d %d", &a, &b, &c);

    largest = (a >= b) ? ((a >= c) ? a : c) : ((b >= c) ? b : c);

    printf("Largest number is %d\n", largest);
    return 0;
}
This method uses the ternary operator for a compact one-line assignment but may be harder for beginners to read.

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

Time Complexity

The program uses a fixed number of comparisons (at most two), so it runs in constant time regardless of input.

Space Complexity

Only a few variables are used to store input and the largest number, so space usage is constant.

Which Approach is Fastest?

All approaches run in constant time; ternary operator is concise but nested if-else may be clearer for beginners.

ApproachTimeSpaceBest For
Simple if-elseO(1)O(1)Beginners, clarity
Nested if-elseO(1)O(1)Slightly fewer comparisons in some cases
Ternary operatorO(1)O(1)Compact code, experienced programmers
💡
Always check all pairs carefully to avoid missing the largest number.
⚠️
Beginners often forget to use logical AND && when comparing the first number with both others.