Ayush
Ayush

Reputation: 49

C: How to print fahrenheit to celsius table in float values

I'm writing a function in C to print a table of fahrenheit to celsius table. Code:

#include <stdio.h>

//code for temprature coversion written in a function

main(){
    int i;

    for (i = 0; i < 201; i = i + 20){
        printf("%d %d\n", i, celsius(i));

    }
    return 0;
}


int celsius(int fahr){

    int i, n, p;

    n = 10;

    p = ((5 * (fahr - 32)) / 9);

    return p;

}

Output:

0 -17
20 -6
40 4
60 15
80 26
100 37
120 48
140 60
160 71
180 82
200 93

The output only contains integers and no float values. How do I get it to print float values?

Upvotes: 0

Views: 777

Answers (2)

Nazim
Nazim

Reputation: 406

1.Function should return float

2.Printf format should be float format

#include <stdio.h>

//code for temprature coversion written in a function
float celsius(int fahr){
   return ((5 * (fahr - 32.0)) / 9);
}

int main()
{
   int i;    
   for (i = 0; i < 201; i = i + 20){
      printf("%3d %5.1f\n", i, celsius(i));
    }
    return 0;
}

Upvotes: 1

JamieT
JamieT

Reputation: 1187

A few things here: Your celsius method returns an integer. You'll want it to return a float:

int celsius(int fahr) to float celsius(int fahr)

C also uses integer division, so unless you tell it otherwise, it will always return an integer. We can fix this by dividing by 9.0 instead of 9 to tell the code we don't want an integer back. We can also clean this function to one line.

float celsius(int fahr){
    return ((5 * (fahr - 32)) / 9.0);
}

In your printf in main, the %d is the format specifier for an integer. If you want to print a float, you need to use the float format specifier of %f. Lets also tell it to print to 1 decimal spot by using the specifier %.1f

printf("%d %.1f\n", i, celsius(i));

with these changes, the code looks like:

float celsius(int fahr){
    return ((5 * (fahr - 32)) / 9.0);
}

main(){
    int i;
    for (i = 0; i < 201; i = i + 20){
        printf("%d %.1f\n", i, celsius(i));

    }
    return 0;
}

now we get the output

0 -17.8                                                                                                                                                                              
20 -6.7                                                                                                                                                                              
40 4.4                                                                                                                                                                               
60 15.6                                                                                                                                                                              
80 26.7                                                                                                                                                                              
100 37.8                                                                                                                                                                             
120 48.9                                                                                                                                                                             
140 60.0                                                                                                                                                                             
160 71.1                                                                                                                                                                             
180 82.2                                                                                                                                                                             
200 93.3 

Or a more compact solution like @Selbie suggested in the comments, we can clean this up to

int i = 0;
for (i = 0; i < 201; i = i + 20){
  printf("%3d %5.1f\n", i, (5*i)/9.0);
}

Upvotes: 3

Related Questions