Skip to lab content

UNIT 01 · Simple numeric problems

Maximum and minimum of three numbers

EXERCISE 01AC173 sample runs

THE SYLLABUS QUESTION

What you need to solve

Write a program for finding the max and min from the three numbers.
Input format & conventions

Three integers between -1,000,000,000 and 1,000,000,000, separated by spaces.

UNDERSTAND THE IDEA

Explanation

Begin with the first value as both the minimum and maximum. Compare the other two values independently with each current result.

Independent comparisons handle ties and negative values naturally. No sorting is necessary.

PLAN BEFORE CODING

Algorithm

  1. Read and validate three integers.
  2. Set minimum and maximum to the first number.
  3. Compare the second and third numbers with both results.
  4. Print the maximum and minimum.

SEE THE CONTROL FLOW

Flowchart

Follow the arrows from Start. Diamonds ask a question; labeled arrows show the answer. A returning arrow repeats a loop. Function internals are grouped where needed; later input checks follow the rules in the program.

Flowchart for Maximum and minimum of three numbers: input, decisions, processing, output and loop paths

On a phone, scroll sideways to read the diagram at full size. Open full-size flowchart ↗

C17

Complete C program

Download .c
#include <stdio.h>

int main(void) {
    long long a, b, c, minimum, maximum;
    if (scanf("%lld %lld %lld", &a, &b, &c) != 3 ||
        a < -1000000000LL || a > 1000000000LL ||
        b < -1000000000LL || b > 1000000000LL ||
        c < -1000000000LL || c > 1000000000LL) {
        puts("Invalid input.");
        return 1;
    }
    minimum = maximum = a;
    if (b < minimum) minimum = b;
    if (c < minimum) minimum = c;
    if (b > maximum) maximum = b;
    if (c > maximum) maximum = c;
    printf("Maximum: %lld\nMinimum: %lld\n", maximum, minimum);
    return 0;
}
Open in compiler ↗

Code loads into the existing compiler. Enter the sample input there; sign-in and execution rules stay the same.

Compile and run locally
gcc -std=c17 maximum-minimum-three.c -o lab
./lab

On Windows, run .\lab.exe after compiling with GCC. The interest program requires the math library where applicable.

FOLLOW THE VALUES

Dry run

Step / stateOperationResult
Start: 12minimum = 12maximum = 12
Read -3minimum = -3maximum = 12
Read 27minimum = -3maximum = 27

CHECK THE BEHAVIOR

Sample input & output

Each output below was produced by compiling and running this exact program. Input values are entered in the stated order; the examples do not print input prompts.

Sample 1

INPUT
12 -3 27
OUTPUT
Maximum: 27
Minimum: -3

Sample 2

INPUT
5 5 5
OUTPUT
Maximum: 5
Minimum: 5

Sample 3

INPUT
-8 -2 -15
OUTPUT
Maximum: -2
Minimum: -15

Common mistakes

  • Do not initialize the minimum to zero: all-positive inputs would give the wrong answer.
  • Do not use one else-if chain that skips independent comparisons.

WHY THIS GROWTH RATE?

Time and space complexity

Time O(1); auxiliary space O(1).

The input always contains exactly three numbers. The program makes four comparisons after initialization: b and c are each compared with the minimum and the maximum. A larger numeric value does not create extra iterations.

A fixed number of reads, comparisons and assignments gives O(1) time. The five scalar variables occupy constant working space, so auxiliary space is O(1).

Big-O describes how work grows as the stated input quantity grows; fixed factors and lower-order terms are omitted. The analysis treats fixed-width arithmetic as constant cost and the published limits as practical safety bounds.