C program to Find the Largest Number Among Three Numbers

This program takes three numbers and finds the largest among them. We will use conditional statements to compare the numbers and determine which one has the maximum value.

Syntax

if (condition1) {
    if (condition2) {
        // statements
    } else {
        // statements
    }
} else {
    if (condition3) {
        // statements
    } else {
        // statements
    }
}

Method 1: Using Nested if Statements

This approach uses nested if statements to compare three numbers and find the largest one −

#include <stdio.h>

int main() {
    int a, b, c;
    a = 2;
    b = 4; 
    c = 7;
    
    printf("Numbers: a = %d, b = %d, c = %d
", a, b, c); if (a > b) { if (a > c) { printf("%d is the largest number
", a); } else { printf("%d is the largest number
", c); } } else { if (b > c) { printf("%d is the largest number
", b); } else { printf("%d is the largest number
", c); } } return 0; }
Numbers: a = 2, b = 4, c = 7
7 is the largest number

Method 2: Using Logical Operators

This method uses logical AND operators to simplify the comparison −

#include <stdio.h>

int main() {
    int a = 15, b = 8, c = 12;
    
    printf("Numbers: a = %d, b = %d, c = %d
", a, b, c); if (a >= b && a >= c) { printf("%d is the largest number
", a); } else if (b >= a && b >= c) { printf("%d is the largest number
", b); } else { printf("%d is the largest number
", c); } return 0; }
Numbers: a = 15, b = 8, c = 12
15 is the largest number

Key Points

  • Both methods have O(1) time complexity as they perform constant comparisons.
  • The logical operator method is more readable and handles equal values correctly.
  • Nested if statements require fewer comparisons in some cases but are harder to understand.

Conclusion

Finding the largest among three numbers can be efficiently done using conditional statements. The logical operator approach is preferred for its clarity and correct handling of equal values.

Updated on: 2026-03-15T11:35:59+05:30

877 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements