I would like to convert everything between -50 degrees and 200 degrees Celsius, in steps of 10 degrees, to Fahrenheit, and print to the user not only both values but also which one is bigger/smaller or whether they are equal. My current code is:
#include <stdio.h>
main()
{
int fahr;
for(fahr = -50; fahr < 201; fahr = fahr + 10)
{
double celsius = (5.0/9.0) * (fahr-32);
if(fahr == celsius)
{
printf("%3d = %6.1f\n", fahr, celsius);
}
if(fahr > celsius)
{
printf("%3d > %6.1f\n", fahr, celsius);
}
if(fahr < celsius)
{
printf("%3d < %6.1f\n", fahr, celsius);
}
}
}
My basic thought process was that I would need to use a for statement, with a few ifs in there for lesser than, greater than and equal to. However, whenever I use the if statements celsius only comes out as one number, and farenheit doesn’t jump by ten after each conversion =/ So i am kinda at a loss as to what to do.
Edit: Alright so, kinda idiodic mistake I used = instead of ==. I’m new to programming, so i’m still trying to get it through my head that = doesn’t actually mean equals. Anyways, that at least fixes the farenheit not going in correct intervals, but celsius still stays at 2.2 instead of changing each time.
EDITEDIT: Well, thanks for the help, turns out I just need to learn when to use the correct data types! I changed the code to its final form in case anyone ever needs it!
The line
if(fahr = celsius)doesn’t do what you think. It assignscelsiustofahrand checks whether the result is non-zero.What you meant is:
This will compare the two values for equality.
As a completely unrelated point of accuracy, you should write the conversion like this:
This will not require any expensive conversion to floating point or rounding errors on the conversion back to integer, and it will delay the division as long as possible. Note of course that your Celsius value will only be approximate to the nearest integer towards zero.