In C/C++ the front-slash ( / ) is used for mathematical division. Generally this will give the integer output. For Example,
void main()
{
int valX = 10 ;
int valY = 3 ;
printf("%f", valX / valY) ;
}
This will print 3.0000000 only instead of 3.3333333, even if you use %f. But if you want accurate float value like 3.3333333, you have to use float variable/value for divisor or divider. For Example
void main()
{
printf("%f", 10.0 / 3) ;
printf("%f", 10 / 3.0) ;
}
Both the print will give 3.3333333. This called Float Division
Subscribe to:
Post Comments (Atom)