### Explain my output

HI, this is my code,

 ``123456789101112131415161718192021222324`` ``````#include #include #include using namespace std; int main(){ double x,y; int quotient; char choice = 'X'; while(toupper(choice) != 'E'){ cout<<"Enter the value of x: "; cin>> x; cout<<"Enter the value of y: "; cin>> y; quotient = x/y; cout <> choice; } return 0; }``````

Can someone explain to me when the value of x=5 and y=2, the output is 2?
Simmilar,

x=6, y=3....output ans is 3
x=19, y=2....output ans is 9

Is it because I declare quotient as int and the number is rounded off to a whole?
When you write x/y with both x and y integers, you are doing integer division. This truncates the fractional portion of any result you get. Thus, 5/2=2.5 truncates to 2 and 19/2=9.5 truncates to 9. I can't explain the other result though...are you sure you read it correctly?
quotient is declared as an integer. It should be a double as well since x and y are doubles. Even if x and y aren't being converted to ints before the division, the answer is being forced into one so you are losing anything after the decimal.
Last edited on
Topic archived. No new replies allowed.