# Convert a given temperature to another system based on given boiling and freezing points

• Last Updated : 04 Jun, 2022

There are two thermometers and given five integers F1, B1, F2, B2, and T, where F1 and B1 are the Freezing points and Boiling Point of water on thermometer 1, and F2 and B2 are the Freezing points and Boiling Point of water on thermometer 2 respectively, and T is some temperature recorded on thermometer 1. The task is to find the temperature on the thermometer 2.

Example:

Input: F1 = 0, B1 = 10, F2 = 100, B2 = 200, T = 4
Output: 140.00

Input: F1 = 0, B1 = 100, F2 = 32, B2 = 212, T = 37
Output: 98.60

Approach: Consider the first thermometer to use U1 unit system and second thermometer using U2 unit system.

• The idea is to get the difference between the boiling and freezing point of water on each thermometer.
• The number of units between the freezing and boiling points of both thermometers shows the same temperature difference.

So, (B1 – F1) U1 == (B2 – F2) U2
By Unitary method, U1 = ( B2 – F2 ) / ( B1 – F1 ) U2
Relative value of U2 is T – F1 and U1 is T – F2
Hence, T = F2 + ( ( B2 – F2 ) / ( B1 – F1 ) ) * ( T – F1 )

Below is the implementation of the above approach:

## C++

 `// C++ program for above approach``#include ``using` `namespace` `std;` `// Function to return temperature``// in the second thermometer``double` `temp_convert(``int` `F1, ``int` `B1, ``int` `F2,``                    ``int` `B2, ``int` `T)``{``    ``float` `t2;` `    ``// Calculate the temperature``    ``t2 = F2 + (``float``)(B2 - F2) /``                     ``(B1 - F1) *``                     ``(T - F1);` `    ``return` `t2;``}` `// Driver Code``int` `main()``{``    ``int` `F1 = 0, B1 = 100;``    ``int` `F2 = 32, B2 = 212;``    ``int` `T = 37;``    ``float` `t2;` `    ``cout << temp_convert(F1, B1, F2, B2, T);``    ``return` `0;``}` `// This code is contributed by kirti`

## C

 `// C program for above approach` `#include ` `// Function to return temperature``// in the second thermometer``double` `temp_convert(``int` `F1, ``int` `B1, ``int` `F2,``                    ``int` `B2, ``int` `T)``{``    ``float` `t2;` `    ``// Calculate the temperature``    ``t2 = F2``         ``+ (``float``)(B2 - F2)``               ``/ (B1 - F1) * (T - F1);` `    ``return` `t2;``}` `// Driver Code``int` `main()``{``    ``int` `F1 = 0, B1 = 100;``    ``int` `F2 = 32, B2 = 212;``    ``int` `T = 37;``    ``float` `t2;` `    ``printf``(``"%.2f"``,``           ``temp_convert(F1, B1, F2, B2, T));``    ``return` `0;``}`

## Java

 `// Java program for above approach``import` `java.io.*;` `class` `GFG{` `// Function to return temperature``// in the second thermometer``static` `double` `temp_convert(``int` `F1, ``int` `B1, ``int` `F2,``                           ``int` `B2, ``int` `T)``{``    ``float` `t2;` `    ``// Calculate the temperature``    ``t2 = F2 + (``float``)(B2 - F2) /``                     ``(B1 - F1) *``                      ``(T - F1);` `    ``return` `t2;``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int` `F1 = ``0``, B1 = ``100``;``    ``int` `F2 = ``32``, B2 = ``212``;``    ``int` `T = ``37``;``    ``float` `t2;` `    ``System.out.printf(``"%.2f"``,``                      ``temp_convert(F1, B1, F2, B2, T));``}``}` `// This code is contributed by rishavmahato348`

## Python3

 `# Python3 program for above approach` `# Function to return temperature``# in the second thermometer``def` `temp_convert(F1, B1, F2, B2, T):``    ` `    ``# Calculate the temperature``    ``t2 ``=` `F2 ``+` `((``float``)(B2 ``-` `F2) ``/``                      ``(B1 ``-` `F1) ``*``                       ``(T ``-` `F1))` `    ``return` `t2` `# Driver Code``F1 ``=` `0``B1 ``=` `100``F2 ``=` `32``B2 ``=` `212``T ``=` `37` `print``(temp_convert(F1, B1, F2, B2, T))` `# This code is contributed by Ankita Saini`

## C#

 `// C# program for above approach``using` `System;` `class` `GFG{` `// Function to return temperature``// in the second thermometer``static` `double` `temp_convert(``int` `F1, ``int` `B1, ``int` `F2,``                           ``int` `B2, ``int` `T)``{``    ``float` `t2;` `    ``// Calculate the temperature``    ``t2 = F2 + (``float``)(B2 - F2) /``                     ``(B1 - F1) *``                      ``(T - F1);` `    ``return` `t2;``}` `// Driver Code``public` `static` `void` `Main()``{``    ``int` `F1 = 0, B1 = 100;``    ``int` `F2 = 32, B2 = 212;``    ``int` `T = 37;``    ``//float t2;` `    ``Console.Write(String.Format(``"{0:0.##}"``,``    ``temp_convert(F1, B1, F2, B2, T)));``}``}` `// This code is contributed by subhammahato348`

## Javascript

 ``

Output:

`98.60`

Time Complexity: O(1), since there is no loop and basic arithmetic is performed.
Auxiliary Space: O(1), since no extra space has been taken.

My Personal Notes arrow_drop_up