WAP to convert temp from Farenheit to Celsius
Objective
Write a C++ program to convert temperature from Fahrenheit to Celsius.
Algorithm / Approach
- Declare float variables for
f(Fahrenheit) andc(Celsius). - Read the Fahrenheit value from the user.
- Apply the mathematical conversion formula:
c = (5.0 / 9.0) * (f - 32). - Print the calculated Celsius temperature.
main.cpp
#include<iostream>
using namespace std;
int main() {
float c,f;
cout<<"Enter Temp in (F): ";
cin>>f;
c = (5.0/9.0)*(f-32);
cout<<"Temp in Celcious = "<< c;
return 0;
}
Expected Output
Enter Temp in (F): 35 Temo in Celcious = 1.6667
Explanation of the Program
- The mathematical formula for converting Fahrenheit to Celsius is C = (5/9) * (F - 32).
- CRITICAL DETAIL: In C++, dividing two integers (like 5 / 9) performs integer division, which discards the decimal and results in exactly 0. By writing
5.0 / 9.0, we force the compiler to perform floating-point division, preserving the crucial 0.5555 fraction.
Complexity
Time Complexity
O(1)
Space Complexity
O(1)