# Temperature Converter

### By Rolo on Nov 26, 2009

This is my solution to Chapter 5 exercise 8 of "C Primer Plus: 5th Edition"
Aside from that, it's a handy program to have around, as temperature conversions always annoy me for some reason.

As the book title implies, this is a C program (hawkee doesn't give a C platform option).
Compile and run with your favorite C compiler. It converts from fahrenheit to celsius and kelvin, then displays all 3 values in float format with .2 precision.

``````#include <stdio.h>

void Temperatures(double n);

int main() {
double temperature;

printf("Enter a temperature in Fahrenheit: ");

while (scanf("%lf", &temperature) == 1) {
Temperatures(temperature);
printf("Enter a temperature in Fahrenheit (a non-number to quit): ");
}

return 0;
}

void Temperatures(double fahrenheit) {
const double CELSIUS1 = 1.8;
const double CELSIUS2 = 32.0;
const double KELVIN1 = 273.15;

double celsius, kelvin;
celsius = fahrenheit * CELSIUS1 + CELSIUS2;
kelvin = celsius + KELVIN1;

printf("Fahrenheit: %.2lf\n", fahrenheit);
printf("Celsius: %.2lf\n", celsius);
printf("Kelvin: %.2lf\n", kelvin);
}
``````

jameswald   -  Oct 19, 2010

@Rolo Thanks a lot for the code. I'm on this exercise, figured everything out except for how to get the while loop to quit when a non-numeric value is entered. I took your while loop arguments and they work but could you explain HOW they work? It seems to be checking that the user input is equal to "1" but how does that work when a user doesn't enter a "1"? Thanks.

Bobknowllmen   -  Apr 26, 2010

how do u convert that to assembly language program?

Rolo   -  Nov 29, 2009

@^Neptune
It's a function prototype, telling the compiler Temperatures() is defined after its first usage (which is in main)

^Neptune   -  Nov 29, 2009

I can understand most of this, but what is "void Temperatures(double n);"?

Rolo   -  Nov 28, 2009

Nah, you're right (outside the limitations of the exercise)
I would either use #define's or your example. Macros are win.

Armand   -  Nov 28, 2009

Then you may delete that comment.

Rolo   -  Nov 28, 2009

Probably should've commented with this when I posted the snippet, here's the guidelines for the programming exercise:

8)
Write a program that requests the user to enter a Fahrenheit temperature. The program should read the temperature as a type double number and pass it as an argument to a user-supplied function called Temperatures(). This function should calculate the Celsius equivalent and the Kelvin equivalent and display all three temperatures with a precision of two places to the right of the decimal. It should identify each value with the temperature scale it represents. Here is the formula for converting Fahrenheit to Celsius:

Celsius = 1.8 * Fahrenheit + 32.0

The Kelvin scale, commonly used in science, is a scale in which 0 represents absolute zero, the lower limit to possible temperatures. Here is the formula for converting Celsius to Kelvin:

Kelvin = Celsius + 273.16

The Temperatures() function should use const to create symbolic representations of the three constants that appear in the conversions. The main() function should use a loop to allow the user to enter temperatures repeatedly, stopping when a q or other nonnumeric value is entered.

Armand   -  Nov 28, 2009

Remove all 3 constants, as they're not needed in this short of code. A numeric value that is hard-coded isn't going to change. Now if you just used a double NAME, then it might change. So for the Temperature function:

void Temperatures(double fahrenheit) {
double celsius, kelvin;

``````celsius = fahrenheit * 1.8 + 32.0;
kelvin = celsius + 273.15;

printf("Fahrenheit: %.2lf\n", fahrenheit);
printf("Celsius: %.2lf\n", celsius);
printf("Kelvin: %.2lf\n", kelvin);``````

}

Rolo   -  Nov 26, 2009

When you're right, you're right :(

Armand   -  Nov 26, 2009

*Temperature, my son

Kelvin constant is 273.15

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.