Responsive Ads Here

Friday, August 5, 2016

How to Print Fractional Numbers in C


If we want show a fractional no. like 54.839373 this. The codes will be,

#How to Print Fractional Numbers in C


Code:
#include <stdio.h>  
 int main()  
 {  
     double a =  54.839373; 
     printf("%lf", a);  
     return 0;  
 } 
Output:


Run the program and see the output. The program prints the numbers correctly. Now, write the following codes again replacing the fractional number by 46.664765767.


Code:
#include <stdio.h>  
 int main()  
 {  
     double a =  54.83559373; 
     printf("%lf", a);  
     return 0;
 }
Output:


Run the program again and see the result. Look, the program can not make the correct result. Now write the codes in bellow,
#include <stdio.h>  
 int main()  
 {  
     double a =  54.83559373; 
     printf("%.8lf", a);  
     return 0;  
 }
Output:


Run the program and see the result again. This time, program are presenting the correct number. Now think why it’s happened? Look the printf quotation line. We wrote .9 to print the 9 digits after the point. As the number contains 9 digits after the point so we’ve to write the total number of digits or more after the point.


Now write a program to print 76.8686765977.

To know more about this topic,

No comments:

Post a Comment