Find the exact average of two whole numbers.
What to build
A program in which integer division would give the wrong answer.
Requirements
- Read two whole numbers.
- Print their average with one decimal place.
Input
One line with two whole numbers.
Output
One number.
Constraints
Each number is from -1000 to 1000.
Example
Input:
3 4
Output:
3.5
Why it works
With two ints, / drops the fraction, so one side must be a double before the division happens.
Examples
Example 1 · Input
3 4
Expected output
3.5
Example 2 · Input
10 20
Expected output
15.0
Hints
Hint 1
Divide by 2.0, or cast one side to double.
Hint 2
%.1f prints one decimal place.
Solution
Show one correct solution (try it yourself first)
#include <stdio.h>
int main(void) {
int a, b;
scanf("%d %d", &a, &b);
double average = (a + b) / 2.0;
printf("%.1f\n", average);
return 0;
}