Learning by doing: Reading books and trying to understand the (code) examples
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

55 lines
1.0 KiB

#include <stdio.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
double getNumber(char * s);
void calcTemperatures(double fahrenheit);
int main(void) {
double fahrenheit;
printf("This program calculates Celsius & Kelvin from Fahrenheit.\n");
fahrenheit = getNumber("Now enter the temp in Fahrenheit: ");
while (!isnan(fahrenheit)) {
calcTemperatures(fahrenheit);
fahrenheit = getNumber("Now enter the first operand (q to quit): ");
};
return 0;
}
double getNumber(char * s) {
double number;
printf("%s", s);
if (!scanf("%lf", &number) || isnan(number)) {
number = NAN;
}
return number;
}
void calcTemperatures(double fahrenheit) {
const float celsius_div = 5.0 / 9.0;
const float celsius_offset = 32.0;
const float kelvin_offset = 273.16;
const double celsius = celsius_div *(fahrenheit - celsius_offset);
const double kelvin = celsius + kelvin_offset;
printf("%.2lf Fahrenheit are %.2lf Celsius or %.2lf Kelvin.\n", fahrenheit, celsius, kelvin);
}