Decimals
Completed
Print the value 22.0/7 three ways on separate lines: with %f, with 2 decimals (%.2f) and in a field of width 8 with 3 decimals (%8.3f).
Input
None.
Output
Three lines.
Example
Output
3.142857 3.14 3.143
Editor
C
Hints
%f prints 6 decimals by default.
The number before the dot is the minimum width.
Solution explanation
Width pads on the left with spaces; precision sets the digits after the decimal point.
Show solution
#include <stdio.h>
int main() {
double x = 22.0 / 7;
printf("%f\n", x);
printf("%.2f\n", x);
printf("%8.3f\n", x);
return 0;
}