C bootcamp · Lab 35

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 — A tie prints that value.
Input
3 3
Output
3
Hint
  1. printf("%d\n", a < b ? a : b);
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;
  if (scanf("%d %d", &a, &b) == 2) {
    printf("%d\n", a < b ? a : b);
  }
  return 0;
}
main.cC17 · gcc · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.