When I wrap 5/9
in parenthesis, the result is 0, but when I don't put in the parenthesis, the result is correct. Why is this?
#include <iostream>
int main()
{
double temperature_farenheit = 195;
std::cout << "Enter the temperature in Farenheit: ";
std::cin >> temperature_farenheit;
double temperature_celsius;
temperature_celsius = (temperature_farenheit - 32) * (5 / 9);
std::cout << "The temperature in Celsius is: " << temperature_celsius;
return 0;
}
Output:
Enter the temperature in Farenheit: 47
The temperature in Celsius is: 0
#include <iostream>
int main()
{
double temperature_farenheit = 195;
std::cout << "Enter the temperature in Farenheit: ";
std::cin >> temperature_farenheit;
double temperature_celsius;
temperature_celsius = (temperature_farenheit - 32) * 5 / 9;
std::cout << "The temperature in Celsius is: " << temperature_celsius;
return 0;
}
Output:
Enter the temperature in Farenheit: 47
The temperature in Celsius is: 8.33333
I did a quick example in Python just to test the result, and the output is correct, but I don't know why the result is incorrect in C++.
temperature_farenheit = 47
temperature_celsius = (temperature_farenheit-32)*(5/9)
print(temperature_celsius)
Output:
8.333333333333334
C++ is incorrect, but why?
#include <iostream>
int main()
{
double temperature_farenheit = 47;
double temperature_celsius = (temperature_farenheit - 32) * (5 / 9);
std::cout << "47F is equal in Celsius to : " << temperature_celsius;
return 0;
}
Output:
47F is equal in Celsius to : 0
I tried putting the fraction at the beginning or at the end. Mathematically both are correct, but for some reason C++ is reporting 0 as a result when I put in parenthesis.