Basic

Switch Case

Goto

Operators

if Statement

Nested if

While Loop

For Loop

Array

2D Array

Patterns

Excersises


C Program to Find the Second Largest Number Among Three Numbers


The following example shows, how to find the second largest number among three given numbers.

  • Takes three integer inputs from the user.
  • Uses nested if statements to compare the three numbers.
  • Determines the second largest number using logical comparisons.
  • Displays the second largest number to the user.

Example : pgm.c

#include <stdio.h>

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

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

    // Use nested if to find the second largest
    if (a > b) {
        if (a > c) {
            // a is the largest
            if (b > c)
                second = b;
            else
                second = c;
        } else {
            // c is larger than a
            second = a;
        }
    } else {
        if (b > c) {
            // b is the largest
            if (a > c)
                second = a;
            else
                second = c;
        } else {
            // c is largest
            second = b;
        }
    }

    printf("Second largest number is: %d\n", second);

    return 0;
}

Output:

Enter three numbers:
10
32
11
Second largest number is: 11