C bootcamp · Lab 03

Celsius to Fahrenheit

easyOperators8 minLesson: C Operators

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

QuestionHint and solution stay closed until you open them

Read a Celsius temperature as an integer. Convert it to Fahrenheit with integer arithmetic:

F = C * 9 / 5 + 32

Print F as an integer. Integer division truncates toward zero, which is what this lab wants.

Input. One integer C.

Output. One integer F.

Constraints

  • -40 ≤ C ≤ 200

Examples

Example 1 — Freezing point of water.
Input
0
Output
32
Example 2 — Boiling point of water.
Input
100
Output
212
Hint
  1. Multiply before you divide so C * 9 / 5 keeps as much precision as integer math allows.
  2. If you write C / 5 * 9, C = 1 becomes 0. Order matters.
Show correct code

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

#include <stdio.h>

int main(void) {
  int c;
  if (scanf("%d", &c) == 1) {
    printf("%d\n", c * 9 / 5 + 32);
  }
  return 0;
}
main.cC17 · gcc · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.