In <(E-Mail Removed)> ashu <(E-Mail Removed)> writes:

> Dear Sir ,

> i am writing a simple C programme but i am facing a problem kindly

> sort it out for me

> #include<stdio.h>

> #include<conio.h>

> main()

> {

> float fahr,celsius;

> celsius= -17;

> fahr=celsius * (9/5) + 32;

> printf("%3.1f\n",fahr);

> getch();

> }

> Result is 15.0

> but when i write: fahr=celsius * 9/5 + 32;

> result is : 1.4

> i can`t figure it out why 1st statement is not working.am i doing

> something wrong.i am using dev C++ compiler .
This expression:

(9/5)

is composed entirely of integers, therefore it is computed using integer

math, which discards the fractional part and evaluates to 1.

So your program is really doing this calculation:

fahr = -17 * 1 + 32

Which evaluates to 15.

To fix this behavior, get in the habit of adding .0 after any number

that you want to be treated as a float, like so:

fahr = celsius * (9.0/5.0) + 32.0;

This will give the correct results.

(Your second example works because without the parentheses, the first

operation is to multiply celsius by 9. One of those numbers is a float,

so the calculation is done as a float. Then that result is divided by

five, and again one of the numbers is a float, so the calculation is done

as a float.)

--

John Gordon A is for Amy, who fell down the stairs

http://www.velocityreviews.com/forums/(E-Mail Removed) B is for Basil, assaulted by bears

-- Edward Gorey, "The Gashlycrumb Tinies"