Find the average of three whole numbers without losing the fraction.
What to build
A program that converts a value at the right moment.
Requirements
- Read three whole numbers.
- Print their sum on one line.
- Print their average on the next, using
static_cast.
Input
One line with three whole numbers.
Output
Two lines.
Constraints
Each number is from -1000 to 1000. Print the average just as cout shows it by default, with up to six significant digits.
Example
Input:
1 2 3
Output:
6
2
Why it works
If the division happened first, the fraction would already be gone before any conversion.
Examples
Example 1 · Input
1 2 3
Expected output
6 2
Example 2 · Input
1 2 4
Expected output
7 2.33333
Hints
Hint 1
Add the three numbers first.
Hint 2
Convert the sum to double before dividing.
Solution
Show one correct solution (try it yourself first)
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cin >> a >> b >> c;
int sum = a + b + c;
cout << sum << endl;
cout << static_cast<double>(sum) / 3 << endl;
return 0;
}