compare celsius degree to fahranhite

Hi everyone,
i was asked to convert celsius degree temp in a range of 0-100
in increment of 5 degree celsius to the equivelant fahrenhite temp and diplay them as 2 colmns without prompting the user i'm getting 100 and 100 as aoutput?. I hope you help with that . thank you

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
int celsius =0;
double fahranhite=0.0,convCels; // I have to declare fahrenhite as
// double
for(int i =0; i < 100; i++)
{
celsius+=5;
fahranhite++;

convCels = 9/5 *(celsius + 32);
}

cout <<"\ncelsius " << celsius <<"\n" << fahranhite <<"\n\n";
return 0;
}



Last edited on
This problem is very common.

9/5

Since 9 and 5 are both integers, this results in integer division. That is, 9/5 is actually 1, not 1.8

If you want it to be 1.8, you need to do floating point division, which means making either 9 or 5 (or both) a floating point:

9.0/5.0

OR, you can do the multiplication first, and the division afterward:

9 * (celsius + 32) / 5;

Either way...
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
  int celsius =0;
  double fahranhite=0.0;

  for(; celsius  <= 100;celsius+=5)
  {
    fahranhite = 9./5. *(celsius + 32);
    cout <<"Celsius: " << celsius <<" Farenheit: " << fahranhite <<endl;
  }

return 0;
}

I've made a few changes here.

First, you don't need to loop i. Just quit when celsius is greater than 100.
Second, you set convCels, but never used it. You need to set fahranhite equal to convCels, or better yet, just drive fahranhite directly.
If you want two columns, put the cout in the loop, otherwise you'll only get the final answer.
Finally, I don't know why you were incrementing fahranhite. It was simply counting up from 0 to 100 and then you sent it out at the end. I think you wanted to set it like you set convCels.
Last edited on
closed account (D80DSL3A)
The conversion formula is incorrect.
It should be: fahranhite = 9./5. *celsius + 32;
thanks stewbond for your comments and thanks for everybody,

but why 0 celsius = 57.6 ??, as you know 0 celsius is = 32 fahrenhite

I think there is somthing wrong with the conversion
Did you see fun2code's post?
oh ya, that make sence. thank you very much
Topic archived. No new replies allowed.