Why is the output of cout << 7/9*9; is zero?

c++, integer-division

Solution

7/9*9 evaluates those numbers as integers, so 7/9 evaluates to 0, and 0*9 = 0.

When you made them floats, you were performing the intended calculation.

Try 7.0/9*9 to get 7, and then you'll be doing a floating point operation.

Problem

Why is the output of the following code equals to 0 or serven? ``` cout << 7/9*9; //output 0 (zero) why? float nine = 9; float seven = 7; float i = seven/nine*nine; cout << i //output 7 Why? ``` Thanks for the help.

Original source

Related problems