Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value not getting right answer

Tags:

c#

int c;
int f = 20;
c = 5 / 9 * (f - 32);
Console.WriteLine(c);
Console.ReadLine();

If I run this code c ends up being 0, which is wrong. Can anyone tell me why?

like image 949
TheAce Avatar asked Jul 10 '26 08:07

TheAce


1 Answers

Because your calculation is being done in integer type. I believe c is double type variable.

c = 5d / 9 * (f - 32.0);

use 32.0 or 32d so that one of the operands is double, also do the same for 5/9.

Also you need to define c as double.

like image 131
Habib Avatar answered Jul 12 '26 01:07

Habib



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!