Menu Close

Leetcode 2469: Convert the Temperature Solution

Here, we will see how to solve Convert the Temperature Solution of leet code 2469 problem.

You are given a non-negative floating point number rounded to two decimal places celsius , that denotes the temperature in Celsius.

You have to convert Celsius into Kelvin and Fahrenheit.

You have to return it as an array ans = [kelvin, fahrenheit].

Answers within 10-5 of the actual answer will be accepted.

Formula:

  • Kelvin = Celsius + 273.15
  • Fahrenheit = Celsius * 1.80 + 32.00

Example 1:

Input: celsius = 41.70
Output: [314.85000, 107.06000]
Explanation: Temperature at 41.70 Celsius converted in Kelvin is 314.85 and converted in Fahrenheit is 107.06.

Example 2:

Input: celsius = 122.11
Output: [395.26000,251.79800]
Explanation: Temperature at 122.11 Celsius converted in Kelvin is 395.26 and converted in Fahrenheit is 251.798.

Convert the Temperature Solution code in C++

Code 1:

class Solution {
public:
    vector<double> convertTemperature(double celsius) {
        cout<<setprecision(6);
        
        double Kelvin = celsius + 273.15;
        double Fahrenheit = celsius * 1.80 + 32.00;
        
        return {Kelvin, Fahrenheit};
    }
};

Code 2:

class Solution {
public:
	vector<double> convertTemperature(double celcius) {
		double kelvin=celcius+273.15;
		double fahrenheit=celcius*1.8+32.0;
		vector<double> ans={kelvin,fahrenheit};
		return ans;
	}
};

Code 3:

class Solution {
public:
    vector<double> convertTemperature(double celsius) {
        
        vector<double> ans;
        ans.push_back(273.15+celsius);
        
        double f=9*celsius/5+32;
        ans.push_back(f);
        
        return ans;
    }
};

Output:

Input: celsius = 41.70
Output: [314.85000, 107.06000]

To check more leetcode problem’s solution. Pls click given below link:

https://techieindoor.com/category/leetcode/

https://techieindoor.com/category/leetcode/

Posted in C++, Easy, Leetcode

Leave a Reply

Your email address will not be published. Required fields are marked *