Conversion from Farenheit to Centigrade always gives zero

2

This is the exercise:

  

1.12.3. The conversion from Farenheit to Celsius degrees is   5   C = 9 (F - 32)
  Make an algorithm that computes and writes a centigrade table based on Farenheit degrees, ranging from 50 to 150 from 1 in 1.

I made my code like this:

#include <stdio.h>

int main () {

    float F, C;

    for (F=50; F<=150; F++) {
        printf("--------------------------------\n");
        printf("Farenheit = %.0f",F);
        C = (5 / 9) * (F - 32);
        printf("\nConvertido para centígrados = %.2f\n",C);
    }
    printf("\n");
    return 0;
}

I've done something wrong in the code or is it compiler error?

    
asked by anonymous 06.04.2017 / 17:22

1 answer

5

The error "never" is from the compiler, it's always from the programmer.

The problem is that you are mixing floating-point and integer numbers. How much is 5 divided by 9? Give 0, there multiply for anything gives 0.

I've used it to simplify things a little:

#include <stdio.h>

int main () {
    for (float F = 50; F <= 150; F++) {
        printf("--------------------------------\n");
        printf("Farenheit = %.0f\nConvertido para centígrados = %.2f\n", F, (5.0f / 9.0f) * (F - 32.0f));
    }
}

See running on ideone . And at Coding Ground . Also put it on GitHub for future reference .

    
06.04.2017 / 17:34