Work out an area, then keep only its whole part.
What to build
A program that uses two types and converts between them on purpose.
Requirements
- Read the width and the height as doubles.
- Print the area with two decimals.
- Print the whole part of the area, using a cast to
int.
Input
One line with two numbers.
Output
One line: area A whole W.
Constraints
Each number is from 0 to 1000.
Example
Input:
2.5 5
Output:
area 12.50 whole 12
Why it works
A cast to int cuts off the fraction without rounding. Doing it after the multiplication keeps the calculation accurate.
Examples
Example 1 · Input
2.5 5
Expected output
area 12.50 whole 12
Example 2 · Input
3 4
Expected output
area 12.00 whole 12
Hints
Hint 1
Read doubles with %lf.
Hint 2
Assigning a double to an int cuts off the fraction. A cast shows that you meant it.
Hint 3
Multiply first, and cut off the fraction afterwards.
Solution
Show one correct solution (try it yourself first)
#include <stdio.h>
int main(void) {
double w, h;
scanf("%lf %lf", &w, &h);
double area = w * h;
printf("area %.2f whole %d\n", area, (int)area);
return 0;
}