C++ bootcamp · Lab 06

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.

std::max({a, b, c}) needs the initializer-list form (with braces) to take three arguments.

Input. Three integers separated by space or newline.

Output. One integer: the largest.

Examples

Example 1
Input
3 9 4
Output
9
Example 2 — Ties are fine.
Input
5 5 2
Output
5
Hint
  1. #include <algorithm> for std::max.
  2. std::max({a, b, c}) compares all three.
Show correct code

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

#include <iostream>
#include <algorithm>

int main() {
  int a, b, c;
  std::cin >> a >> b >> c;
  std::cout << std::max({a, b, c}) << "\n";
  return 0;
}
main.cppC++17 · g++ · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.