x = 2/4;
printf("%f", x);
Output:
0.00000
How can 2/4 result is 0.000 ?? How can i make the output so that it is 0.5
This is a discussion on Whats wrong ?? within the C Programming forums, part of the General Programming Boards category; x = 2/4; printf("%f", x); Output: 0.00000 How can 2/4 result is 0.000 ?? How can i make the output ...
x = 2/4;
printf("%f", x);
Output:
0.00000
How can 2/4 result is 0.000 ?? How can i make the output so that it is 0.5
I'm assuming X is declared to be an integer, right? The result of 2/4 is 0.5, but as X is in integer, it will simply truncate the result. Instead of .5 you see 0. Simply change X to a float. Also, if you only want it to display 1 or 2 decimal points, make sure you print it by using %.2f, x
An integer divided by an integer gives an integer.
Use:
Code:x = (float) 2 / (float) 4; // OR x = 2.0 / 4.0;