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
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
Example 1: Celsius = 36.50000; Kelvin = 309.64999 ; Fahrenheit = 97.70000 Example 2: Celsius = 122.11000; Kelvin = 395.26001 ; Fahrenheit = 251.79799
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
At line 7 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7ee5e0412960 in ??? #1 0x7ee5e04134d9 in ??? #2 0x7ee5e066717b in ??? #3 0x7ee5e0660684 in ??? #4 0x7ee5e06612aa in ??? #5 0x597e207b8209 in MAIN__ #6 0x597e207b841c in main
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.
#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.