Deitel & Deitel, C How to Program 6th Edition, Exercise 9.10 (Printing Numbers in Various Field Widths)
// 9.10 Printing Numbers in Various Field Widths
#include <stdio.h>
int main() {
int intNum = 12345;
float floatNum = 123.4567;
printf("DECIMALS\n");
printf("%8d", intNum);
printf("-->If field width is more than digits\n");
printf("%5d", intNum);
printf("----->If field width is equal to digits\n");
printf("%3d", intNum);
printf("----->If field width is less than digits\n");
printf("\nFLOATS\n");
printf("%10f", floatNum);
printf("-->If field width is more than digits\n");
printf("%7f", floatNum);
printf("-->If field width is equal to digits\n");
printf("%2f", floatNum);
printf("-->If field width is less than digits\n");
printf("%1.1f", floatNum);
printf("------->If field width is less than digits, and precision is less than digits after the decimal point\n");
printf("%3.5f", floatNum);
printf("--->If field width is less than digits, and precision is equal to digits after the decimal point\n");
printf("%8.2f", floatNum);
printf("---->If field width is more than digits, and precision is less than digits after the decimal point\n");
printf("\n\n\n");
return 0;
}
OUTPUT
DECIMALS
12345-->If field width is more than digits
12345----->If field width is equal to digits
12345----->If field width is less than digits
FLOATS
123.456703-->If field width is more than digits
123.456703-->If field width is equal to digits
123.456703-->If field width is less than digits
123.5------->If field width is less than digits, and precision is less than digits after the decimal point
123.45670--->If field width is less than digits, and precision is equal to digits after the decimal point
123.46---->If field width is more than digits, and precision is less than digits after the decimal point
Press any key to continue . . .
#include <stdio.h>
int main() {
int intNum = 12345;
float floatNum = 123.4567;
printf("DECIMALS\n");
printf("%8d", intNum);
printf("-->If field width is more than digits\n");
printf("%5d", intNum);
printf("----->If field width is equal to digits\n");
printf("%3d", intNum);
printf("----->If field width is less than digits\n");
printf("\nFLOATS\n");
printf("%10f", floatNum);
printf("-->If field width is more than digits\n");
printf("%7f", floatNum);
printf("-->If field width is equal to digits\n");
printf("%2f", floatNum);
printf("-->If field width is less than digits\n");
printf("%1.1f", floatNum);
printf("------->If field width is less than digits, and precision is less than digits after the decimal point\n");
printf("%3.5f", floatNum);
printf("--->If field width is less than digits, and precision is equal to digits after the decimal point\n");
printf("%8.2f", floatNum);
printf("---->If field width is more than digits, and precision is less than digits after the decimal point\n");
printf("\n\n\n");
return 0;
}
OUTPUT
DECIMALS
12345-->If field width is more than digits
12345----->If field width is equal to digits
12345----->If field width is less than digits
FLOATS
123.456703-->If field width is more than digits
123.456703-->If field width is equal to digits
123.456703-->If field width is less than digits
123.5------->If field width is less than digits, and precision is less than digits after the decimal point
123.45670--->If field width is less than digits, and precision is equal to digits after the decimal point
123.46---->If field width is more than digits, and precision is less than digits after the decimal point
Press any key to continue . . .
Comments
Post a Comment