C Program to Find Largest of 3 Numbers – 3 Methods

Finding the largest of three numbers is one of the first real decisions a C program has to make. It sounds simple, but it teaches you conditional logic, how the if-else ladder works, and why wrapping repeated logic in a function pays off immediately. This post shows three approaches — a plain if-else, a ternary one-liner, and a reusable function — so you can see how each trades off readability against compactness.

Method 1 — if-else Ladder

The most readable approach. Check each number in turn; the first condition that is true wins.

#include <stdio.h>

int main(void)
{
    int a, b, c;

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

    if (a >= b && a >= c)
        printf("Largest: %d\n", a);
    else if (b >= a && b >= c)
        printf("Largest: %d\n", b);
    else
        printf("Largest: %d\n", c);

    return 0;
}

Method 2 — Ternary Operator

A nested ternary resolves the same logic in a single expression. Useful when you need the result as a value rather than printing directly.

#include <stdio.h>

int main(void)
{
    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: %d\n", largest);

    return 0;
}

Method 3 — Reusable Function

When the same comparison appears in multiple places, pull it into a function. main() stays clean, and the logic is easy to test in isolation.

#include <stdio.h>

int largest_of_three(int a, int b, int c)
{
    if (a >= b && a >= c)
        return a;
    if (b >= a && b >= c)
        return b;
    return c;
}

int main(void)
{
    int a, b, c;

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

    printf("Largest: %d\n", largest_of_three(a, b, c));
    return 0;
}

How to Compile and Run

gcc -ansi -Wall -Wextra -o largest largest.c
./largest

Sample Input and Output

Enter three numbers: 14 72 31
Largest: 72
Enter three numbers: -5 -5 -5
Largest: -5
Enter three numbers: 100 100 99
Largest: 100

Code Explanation

Method 1 uses a two-condition check for each candidate: a >= b && a >= c confirms that a is at least as large as both others. Using >= instead of > handles ties correctly — when two or three numbers are equal, the first matching branch wins and any of the equal values is printed, which is correct.

Method 2 nests two ternary expressions. Read it inside-out: (a >= c) ? a : c picks the larger of a and c; the outer ternary selects that result only when a >= b, otherwise falling through to the b vs c comparison.

Method 3 separates the concern from main(). The function returns early as soon as a winner is identified — no else needed because return exits the function immediately. If neither a nor b wins, c must be largest (or tied for largest), so the final return c is always correct.

What This Program Teaches

  • How if-else ladders evaluate conditions top to bottom and stop at the first true branch
  • How to use the ternary operator ? : to produce a value from a condition
  • Why returning early from a function is cleaner than deeply nested else blocks
  • How to handle ties: using >= instead of > makes all three methods correct when numbers are equal

Related C Programs

📖 Learning C from a book? The C Programming Language by K&R (Amazon.in) · Amazon.com

\\Preparing\ for\ a\ C\ interview\?\ Practice\ with\ the\ \C\ Programming\ Quiz\\ —\ 150+\ questions,\ free\ on\ Android.\\

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>