#include<stdio.h>
#include<conio.h>
int main()
{
printf("\n %5d",12);
printf("\n %5d",123);
printf("\n %5d",1234);
printf("\n %4.5f",6.12);
printf("\n %4.6f",16.12);
printf("\n %4.7f",167.12);
printf("\n %4.8f",1678.12);
printf("\n %8s","Saloni Singh");
printf("\n %8.9s","Saloni Singh");
getche();
}
The program you have written demonstrates the use of width and precision specifiers for formatting integers, floating-point numbers, and strings.
The output of the program is:
12
123
1234
6.12000
16.120000
167.1200000
1678.12000000
Saloni Singh
Saloni Si
Explanation:
%5d
: This specifier specifies that the integer should be printed with a width of at least 5 characters. If the integer is less than 5 characters wide, it will be padded with spaces on the left. In this case, the integers 12, 123, and 1234 are printed with a width of 5 characters.%4.5f
: This specifier specifies that the floating-point number should be printed with a width of at least 4 characters and a precision of 5 decimal places. If the number is less than 4 characters wide, it will be padded with spaces on the left. In this case, the floating-point numbers 6.12, 16.12, 167.12, and 1678.12 are printed with a width of 4 characters and a precision of 5 decimal places.%8s
: This specifier specifies that the string should be printed with a width of at least 8 characters. If the string is less than 8 characters wide, it will be padded with spaces on the left. In this case, the string “Saloni Singh” is printed with a width of 8 characters.%8.9s
: This specifier specifies that the string should be printed with a width of at least 8 characters and a precision of 9 characters. If the string is less than 8 characters wide, it will be padded with spaces on the left. In this case, the string “Saloni Singh” is printed with a width of 8 characters and a precision of 9 characters, so only the first 9 characters of the string are printed.
Thanks
Write a program to display the integers,float point numbers and string with different formats and explained above.