Skip to main content

ProwessApps

Learn · Practice · Excel

WAP to convert temp from Farenheit to Celsius

C++ Code Example — Basic Programs

ADVERTISEMENT

WAP to convert temp from Farenheit to Celsius

Objective

Write a C++ program to convert temperature from Fahrenheit to Celsius.

Algorithm / Approach

  1. Declare float variables for f (Fahrenheit) and c (Celsius).
  2. Read the Fahrenheit value from the user.
  3. Apply the mathematical conversion formula: c = (5.0 / 9.0) * (f - 32).
  4. 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)
ADVERTISEMENT