C bootcamp · Lab 47

Median of three

easyIf Else10 minLesson: If Else

Read the question, write C on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Read three integers and print the median.

Input. Three integers.

Output. The middle value.

Examples

Example 1
Input
3 1 2
Output
2
Example 2
Input
9 9 1
Output
9
Hint
  1. Sort into three temps, or compare pairwise.
Show correct code

Peek only after you have tried. You can still Check your own version.

#include <stdio.h>

int main(void) {
  int a, b, c;
  if (scanf("%d %d %d", &a, &b, &c) != 3) return 1;
  if (a > b) { int t = a; a = b; b = t; }
  if (b > c) { int t = b; b = c; c = t; }
  if (a > b) { int t = a; a = b; b = t; }
  printf("%d\n", b);
  return 0;
}
main.cC17 · gcc · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.