C bootcamp · Lab 05

Largest 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 largest one.

If two or more values tie for largest, print that value once.

Input. Three integers a, b, and c.

Output. One integer — the maximum.

Constraints

  • -1000 ≤ a, b, c ≤ 1000

Examples

Example 1
Input
3 9 4
Output
9
Example 2 — A tie is fine. Print 5.
Input
5 5 2
Output
5
Hint
  1. Start with max = a, then compare b and c.
  2. You can also nest if statements, or write if (a >= b && a >= c).
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) {
    int max = a;
    if (b > max) max = b;
    if (c > max) max = c;
    printf("%d\n", max);
  }
  return 0;
}
main.cC17 · gcc · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.