Convert the Temperature

🏠 ⬅️ ➡️

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

You should convert Celsius into Kelvin and Fahrenheit and return it as an array ans = [kelvin, fahrenheit].

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

Note that:

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

Example 1:

Input: celsius = 36.50 Output: [309.65000,97.70000] Explanation: Temperature at 36.50 Celsius converted in Kelvin is 309.65 and converted in Fahrenheit is 97.70.

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.

Constraints:

  • 0 <= celsius <= 1000

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    real(kind=4), parameter :: tolerance = 10e-5
    real(kind=4) :: celsius
    real(kind=4), dimension(2) :: ans

    ! Example 1
    celsius = 36.50
    ans = solve_problem(celsius)
    write(*,*) 'Example 1:'
    write(*,'(A,F10.5,A,F10.5)') 'Celsius = ', celsius, &
                                  '; Kelvin = ', ans(1), &
                                  '; Fahrenheit = ', ans(2)

    ! Example 2
    celsius = 122.11
    ans = solve_problem(celsius)
    write(*,*) 'Example 2:'
    write(*,'(A,F10.5,A,F10.5)') 'Celsius = ', celsius, &
                                  '; Kelvin = ', ans(1), &
                                  '; Fahrenheit = ', ans(2)

contains

    function solve_problem(celsius) result(ans)
        implicit none
        real(kind=4), intent(in) :: celsius
        real(kind=4), dimension(2) :: ans

        ans(1) = celsius + 273.15
        ans(2) = celsius * 1.80 + 32.00

    end function solve_problem

end program main
Compiled
Executed
Correct
PROGRAM TEMPERATURE
      IMPLICIT NONE

      REAL(KIND=8) :: celsius, kelvin, fahrenheit, ans(2)

      ! Read input
      READ(*,*) celsius

      ! Calculate Kelvin and Fahrenheit
      kelvin = celsius + 273.15
      fahrenheit = celsius * 1.80 + 32.00

      ! Store results in array
      ans = [kelvin, fahrenheit]

      ! Print output
      WRITE(*,*) ans

      END PROGRAM TEMPERATURE
🌐 Data from online sources
def convert_temperature(celsius):
    kelvin = celsius + 273.15
    fahrenheit = celsius * 1.8 + 32
    return [kelvin, fahrenheit]

The algorithm is quite simple. First, we calculate and store the temperature value in Kelvin by adding 273.15 to the given Celsius value. Next, we calculate the Fahrenheit value by multiplying the Celsius value by 1.8 and adding 32. Finally, we return an array with the calculated Kelvin and Fahrenheit values.

🌐 Data from online sources
#include <vector>

std::vector<double> convertTemperature(double celsius) {
    double kelvin = celsius + 273.15;
    double fahrenheit = celsius * 1.80 + 32.00;
    return {kelvin, fahrenheit};
}

The algorithm is quite simple. First, we calculate and store the temperature value in Kelvin by adding 273.15 to the given Celsius value. Next, we calculate the Fahrenheit value by multiplying the Celsius value by 1.8 and adding 32. Finally, we return an array with the calculated Kelvin and Fahrenheit values.