C bootcamp · Lab 46

Absolute difference

easyMath8 minLesson: Math

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 |a − b|.

Input. Two integers.

Output. One non-negative integer.

Examples

Example 1
Input
10 3
Output
7
Example 2
Input
3 10
Output
7
Hint
  1. Use abs from stdlib.h, or branch on a > b.
Show correct code

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

#include <stdio.h>
#include <stdlib.h>

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