I'm very new to programming in C and I attempted to make a program that converts temperatures to and from Fahrenheit and Celsius. I have been running the program and I have realized that not all conversions are correct. For example, if I use the boiling point of water (100C or 212F), I get an incorrect answer. I thought that I used the correct formulas. I was wondering if you wouldn't mind reviewing this quick program.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
float c;
float f;
int w;
printf("Type 1 if you would like to convert to Farenheit\n");
printf("Type 2 if you would like to convert to Celcius\n");
scanf("%i", &w);
// Loop to make sure value is 1 or 2
while(w != 1 && w !=2) {
fprintf(stderr, "You didn't input a valid number. Please Try Again\n");
scanf("%i", &w);
}
// Celsius --> Fahrenheit
if(w == 1){
printf("Input the Celcius Temperature\n");
scanf("%f", &c);
printf("You entered %f\n", c);
f = c * (9./5.) + 32.;
printf("%f in Fareheit is %f\n", c, f);
}
// Fahrenheit --> Celsius
if(w == 2){
printf("Input the Farenheit Temperature\n");
scanf("%f", &f);
printf("You entered %f\n",f);
c = (f - 32.) * (5./9.);
printf("%f in Celcius is %f\n", f, c);
}
return 0;
}
9/5
and5/9
. The result is 1 and 0, respectively, due to truncation to integer. Use9./5
and5./9
, respectively. \$\endgroup\$