Division in C

Hello,

If I use (5/9) or (9/5) instead of 0.55 and 1.8, I have different output.
What's the problem?

Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<stdio.h>

void FtoC(double F)
{
   // printf("%lf",(F - 32)*(.55));
      printf("%lf",(F - 32)*(5/9));
}
void CtoF(double C)
{

   // printf("%lf",(C*1.8)+32);
   printf("%lf",(C*(5/9))+32);
}

int main()
{
    printf("Press C ==> to Celsius to Fahrenheit\n");
    printf("Press F ==> to Fahrenheit to Celsius\n\n");

    char ch = getchar();

    printf("Number: ");
    double s ;
    scanf("%lf",&s);

    if (ch == 'C')
    {

        CtoF(s);
    }
    else if (ch == 'F')
    {

        FtoC(s);
    }

    	return 0;
}
Dividing two ints does integer division (essentially truncating the fractional part).
At least one operand must be a floating-point number to do floating-point division.
Last edited on
You can try this:
(float)5/(float)9
Or this:
5.0/9.0

Topic archived. No new replies allowed.