Skip to content
Laviyx Learn

Try

PPS Lab · Experiment 3

Maximum and minimum of three numbers

CSimple numeric problemsJNTUH R22 · CS108ES

Aim

To write a C program that reads three integers and finds the largest (maximum) and the smallest (minimum) of them.

Algorithm

  1. Read three integers a, b and c.
  2. Assume a is the maximum and also the minimum.
  3. If b is greater than the maximum, make b the maximum. If c is greater than the maximum, make c the maximum.
  4. If b is less than the minimum, make b the minimum. If c is less than the minimum, make c the minimum.
  5. Print the maximum and the minimum.

Program

C
#include <stdio.h>

int main(void) {
    int a, b, c;
    printf("Enter three integers: ");
    if (scanf("%d %d %d", &a, &b, &c) != 3) {
        printf("Invalid input\n");
        return 1;
    }

    int max = a, min = a;

    if (b > max) {
        max = b;
    }
    if (c > max) {
        max = c;
    }

    if (b < min) {
        min = b;
    }
    if (c < min) {
        min = c;
    }

    printf("Maximum = %d\n", max);
    printf("Minimum = %d\n", min);
    return 0;
}

Output

Three different numbers

Output 1
Enter three integers: 12 45 7
Maximum = 45
Minimum = 7

Negative numbers with a repeated value

Output 2
Enter three integers: -3 -8 -3
Maximum = -3
Minimum = -8

Recorded by compiling and running this exact program. Typed input appears after each prompt.

How it works

Starting with max = min = a and then comparing with the other numbers is simpler than writing every case with nested ifs, and it works even when two or all three numbers are equal.

The same result can be written with the conditional operator: max = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);.

Common mistakes

  • Using else if between the maximum and minimum checks. Then a number that updates the maximum is never compared for the minimum. Keep the two sets of checks separate.

  • Writing if (a > b > c). C evaluates a > b first to get 0 or 1 and then compares that with c, which is not what is meant. Use a > b && a > c.

  • Starting max and min at 0 instead of the first number. With all-negative input such as −3, −8, −3, the maximum would wrongly come out as 0.

  • Forgetting the & before the variables in scanf, which makes the program crash or read nothing.

Viva questions

Try to answer each one aloud before you open it.

  • Why are max and min both set to a at the start?

    The first number is a real value from the input, so it is a safe starting point. Every other number is then compared with it. Starting at 0 fails when all the numbers are negative.

  • How do you find the largest of three numbers using the conditional operator?

    max = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c); First compare a and b, then compare the larger one with c.

  • What does a > b > c mean in C?

    It is read as (a > b) > c. The first part gives 1 or 0, which is then compared with c. To check that a is greater than both, write a > b && a > c.

  • How many comparisons does this program make?

    Four: two to find the maximum and two to find the minimum.

Learn the concepts

All Programming for Problem Solving Lab experiments →