C++ bootcamp · Lab 40

Min with a ternary

easyTernary8 minLesson: Ternary

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

QuestionHint and solution stay closed until you open them

Read two integers and print the smaller one.

Use the ternary operator condition ? a : b rather than an if statement.

Input. Two integers a and b.

Output. The smaller integer.

Examples

Example 1
Input
9 4
Output
4
Example 2
Input
3 3
Output
3
Hint
  1. std::cout << (a < b ? a : b) << "\n";
Show correct code

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

#include <iostream>

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