Solution for Exercise 5-3
ex0503
#include <stdio.h> int main() { printf("%d\n",127); printf("%1.2f\n",3.1415926535); printf("%d\n",122013); printf("%1.1f\n",0.00008); return(0); }
Notes
* Here is the output I see:
127 3.14 122013 0.0
The value of π is displayed with 1 digit to the left of the decimal and 2 digits to the right. The final value is displayed with one digit to both the left and right of the decimal. So where's the .00008? It gets rounded off.
* Change the final printf() statement so that the placeholder is written %1.4f
. Build and run again:
127 3.14 122013 0.0001
The "8" doesn't show up in the output, but its effect on rounding up the next digit is shown.
* Change the second printf() statement's placeholder from %1.2f
to %1.3f
. Build and run again:
127 3.142 122013 0.0001
The value of π is rounded up, from 3.1415 to 3.142.
Copyright © 1997-2024 by QPBC.
All rights reserved