Deitel & Deitel, C How to Program 6th Edition, Exercise 9.11 (Rounding Floating-Point Numbers)
// 9.11 Rounding Floating-Point Numbers
#include <stdio.h>
int main() {
float num = 100.453627;
printf("The exact number is:________________________________%f", num);
printf("\n");
printf("The number rounded to nearest integer:______________");
printf("%.0f", num);
printf("\n");
printf("The number rounded to nearest tenth digit:__________");
printf("%.1f", num);
printf("\n");
printf("The number rounded to nearest hundredth digit:______");
printf("%.2f", num);
printf("\n");
printf("The number rounded to nearest thousandth digit:_____");
printf("%.3f", num);
printf("\n");
printf("The number rounded to nearest ten-thousandth digit:_");
printf("%.4f", num);
printf("\n");
printf("\n\n\n");
return 0;
}
OUTPUT
The exact number is:________________________________100.453629
The number rounded to nearest integer:______________100
The number rounded to nearest tenth digit:__________100.5
The number rounded to nearest hundredth digit:______100.45
The number rounded to nearest thousandth digit:_____100.454
The number rounded to nearest ten-thousandth digit:_100.4536
Press any key to continue . . .
#include <stdio.h>
int main() {
float num = 100.453627;
printf("The exact number is:________________________________%f", num);
printf("\n");
printf("The number rounded to nearest integer:______________");
printf("%.0f", num);
printf("\n");
printf("The number rounded to nearest tenth digit:__________");
printf("%.1f", num);
printf("\n");
printf("The number rounded to nearest hundredth digit:______");
printf("%.2f", num);
printf("\n");
printf("The number rounded to nearest thousandth digit:_____");
printf("%.3f", num);
printf("\n");
printf("The number rounded to nearest ten-thousandth digit:_");
printf("%.4f", num);
printf("\n");
printf("\n\n\n");
return 0;
}
OUTPUT
The exact number is:________________________________100.453629
The number rounded to nearest integer:______________100
The number rounded to nearest tenth digit:__________100.5
The number rounded to nearest hundredth digit:______100.45
The number rounded to nearest thousandth digit:_____100.454
The number rounded to nearest ten-thousandth digit:_100.4536
Press any key to continue . . .
Comments
Post a Comment