Temperature conversion: Difference between revisions

Added Prolog
(Added solution for Action!)
(Added Prolog)
 
(31 intermediate revisions by 17 users not shown)
Line 35:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">V k = 21.0
print(‘K ’k)
print(‘C ’(k - 273.15))
print(‘F ’(k * 1.8 - 459.67))
print(‘R ’(k * 1.8))</langsyntaxhighlight>
 
{{out}}
Line 53:
Use of packed decimal arithmetic
(ZAP,SP,MP,DP,UNPK,CVD,EDMK opcodes).
<syntaxhighlight lang="text">* Temperature conversion 10/09/2015
TEMPERAT CSECT
USING TEMPERAT,R15
Line 125:
EDMASKN DC X'402021204B202060' CL8 5num
YREGS
END TEMPERAT</langsyntaxhighlight>
{{out}}
<pre>
Line 146:
 
=={{header|8th}}==
<langsyntaxhighlight lang="forth">: KtoC \ n -- n
273.15 n:-
;
Line 178:
bye
;
</syntaxhighlight>
</lang>
{{out}}
<pre>>8th temp.8th 21
Line 189:
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
 
PROC K2C(REAL POINTER k,c)
Line 241:
ValR("373.15",k) Test("Water boils",k)
RETURN
</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Temperature_conversion.png Screenshot from Atari 8-bit computer]
Line 266:
=={{header|Ada}}==
 
<langsyntaxhighlight Adalang="ada">with Ada.Float_Text_IO, Ada.Text_IO; use Ada.Float_Text_IO, Ada.Text_IO;
 
procedure Temperatur_Conversion is
Line 279:
Put("F: "); Put(F, Fore => 4, Aft => 2, Exp => 0); New_Line;-- F: dddd.dd
Put("R: "); Put(R, Fore => 4, Aft => 2, Exp => 0); New_Line;-- R: dddd.dd
end;</langsyntaxhighlight>
 
{{out}}
Line 290:
 
=={{header|Aime}}==
<langsyntaxhighlight lang="aime">void
show(integer symbol, real temperature)
{
Line 309:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>aime$ aime -a tmp/tconvert 300
Line 318:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">
BEGIN
REAL kelvin;
Line 326:
printf ((f, kelvin, 9.0 * kelvin / 5.0, "R"));
printf ((f, kelvin, 9.0 * kelvin / 5.0 - 459.67, "F"))
END</langsyntaxhighlight>
{{out}}
<pre>$ echo 21 | a68g Temperature_conversion.a68
Line 336:
=={{header|ALGOL-M}}==
If the temperature in Kelvin is a whole number, you should type a decimal point after it (e.g. <code>290.</code>): <code>290</code> with no decimal point will be interpreted as 0.29 rather than 290.0.
<langsyntaxhighlight lang="algol">BEGIN
DECIMAL K, C, F, R;
WRITE( "Temperature in Kelvin:" );
Line 347:
WRITE( F, " degrees Fahrenheit" );
WRITE( R, " degrees Rankine" );
END</langsyntaxhighlight>
 
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">
begin % convert Kelvin to Celcius, Farenheit and Rankine %
real kelvin, rankine;
write( "Kelvin: " );
read( kelvin );
rankine := ( 9 * kelvin ) / 5;
r_format := "A"; r_w := 8; r_d := 2; s_w := 0; % set output formating %
write( kelvin, " Kelvin is:" );
write( " ", kelvin - 273.15, " Celcius" );
write( " ", rankine, " Rankine" );
write( " ", rankine - 459.67, " Farenheit" )
end.
</syntaxhighlight>
 
=={{header|Amazing Hopper}}==
<syntaxhighlight lang="amazing hopper">
/* MISTRAL - a flavour of Hopper */
 
#include <mistral.h>
 
INICIAR:
TAMAÑO DE MEMORIA 20
temperatura=0
RECIBIR PARÁMETRO NUMÉRICO(2), GUARDAR EN (temperatura);
TOMAR("KELVIN : ",temperatura, NL)
CON( "CELSIUS : ", temperatura ), CALCULAR( Conversión Kelvin a Celsius ), NUEVA LÍNEA
CON( "FAHRENHEIT : ", temperatura ), CALCULAR( Conversión Kelvin a Fahrenheit ), NUEVA LÍNEA
CON( "RANKINE : ", temperatura ), CALCULAR( Conversión Kelvin a Rankine ), NUEVA LÍNEA
IMPRIMIR CON SALTO
FINALIZAR
 
SUBRUTINAS
 
FUNCIÓN(Conversión Kelvin a Celsius, k)
REDONDEAR(RESTAR(k, 273.15), 2)
RETORNAR
 
FUNCIÓN( Conversión Kelvin a Fahrenheit, k)
REDONDEAR( {k} MULTIPLICADO POR(1.8) MENOS( 459.67), 2)
RETORNAR
 
FUNCIÓN( Conversión Kelvin a Rankine, k)
RETORNAR ( {k} POR (1.8), REDONDEADO AL DECIMAL(2) )
</syntaxhighlight>
<p>Another version:</p>
<syntaxhighlight lang="amazing hopper">
/* MISTRAL - a flavour of Hopper */
 
#include <mistral.h>
 
INICIAR:
temperatura=0
RECIBIR PARÁMETRO NUMÉRICO(2), GUARDAR EN (temperatura);
IMPRIMIR("KELVIN : ",temperatura, NL)
IMPRIMIR("CELSIUS : ",temperatura, MENOS '273.15', NL)
IMPRIMIR("FAHRENHEIT : ",temperatura, POR '1.8' MENOS '459.67', NL)
IMPRIMIR("RANKINE : ",temperatura, POR '1.8', NL)
FINALIZAR
</syntaxhighlight>
<p>Another version:</p>
<syntaxhighlight lang="amazing hopper">
#include <mistral.h>
 
INICIAR:
TAMAÑO DE MEMORIA 15
temperatura=0
RECIBIR PARÁMETRO NUMÉRICO(2), GUARDAR EN (temperatura);
IMPRIMIR("KELVIN : ", temperatura, NL,\
"CELSIUS : ", {temperatura} MENOS '273.15', NL,\
"FAHRENHEIT : ", {temperatura} POR '1.8' MENOS '459.67', NL,\
"RANKINE : ", {temperatura} POR '1.8', NL)
FINALIZAR
</syntaxhighlight>
<p>Or another (and last) version:</p>
<syntaxhighlight lang="amazing hopper">
#include <mistral.h>
 
INICIAR:
TAMAÑO DE MEMORIA 20
temperatura=0, rankine=0
RECIBIR PARÁMETRO NUMÉRICO(2), GUARDAR EN (temperatura);
IMPRIMIR("KELVIN : ", temperatura, NL,\
"CELSIUS : ", {temperatura} MENOS '273.15', NL,\
"FAHRENHEIT : ", {temperatura} POR '1.8' ---RESPALDE EN 'rankine'--- MENOS '459.67', NL,\
"RANKINE : ", rankine, NL)
FINALIZAR
</syntaxhighlight>
{{out}}
<pre>
$ hopper conv.mistral 0
KELVIN : 0
CELSIUS : -273.15
FAHRENHEIT : -459.67
RANKINE : 0
</pre>
<pre>
$ hopper conv.mistral 21
KELVIN : 21
CELSIUS : -252.15
FAHRENHEIT : -421.87
RANKINE : 37.8
</pre>
 
=={{header|APL}}==
Given a temperature in Kelvin, prints the equivalent in Kelvin, Celsius, Fahrenheit, and Rankine (in that order).
<langsyntaxhighlight lang="apl"> CONVERT←{⍵,(⍵-273.15),(R-459.67),(R←⍵×9÷5)}</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="apl"> CONVERT 21
21 ¯252.15 ¯421.87 37.8</langsyntaxhighlight>
The "high minus" character <tt>¯</tt> is used in APL to mark negative numbers, preventing any possible confusion with <tt>-</tt> (the subtraction operator).
 
=={{header|AppleScript}}==
{{Trans|JavaScript}} ( ES6 version )
<langsyntaxhighlight AppleScriptlang="applescript">use framework "Foundation" -- Yosemite onwards, for the toLowerCase() function
 
-- KELVIN TO OTHER SCALE -----------------------------------------------------
Line 471 ⟶ 575:
((ca's NSString's stringWithString:(str))'s ¬
lowercaseStringWithLocale:(ca's NSLocale's currentLocale())) as text
end toLower</langsyntaxhighlight>
{{Out}}
<pre>K 21.0
Line 482 ⟶ 586:
Or of course:
 
<langsyntaxhighlight lang="applescript">on convertFromKelvin(kelvinValue)
return ("K" & tab & (kelvinValue as real)) & ¬
(linefeed & "C" & tab & (kelvinValue - 273.15)) & ¬
Line 489 ⟶ 593:
end convertFromKelvin
 
convertFromKelvin(21)</langsyntaxhighlight>
 
Vanilla AppleScript actually has a handful of built-in measurement unit coercions, including three for converting between temperatures in Kelvin, Celsius, and Fahrenheit. There isn't one for Rankine, but it's an easy calculation from Kelvin:
 
<langsyntaxhighlight lang="applescript">on convertFromKelvin(kelvinValue)
set kelvinMeasurement to kelvinValue as degrees Kelvin
Line 506 ⟶ 610:
end convertFromKelvin
 
convertFromKelvin(21)</langsyntaxhighlight>
 
As from macOS 10.12 Sierra, macOS's Foundation framework too offers "Units and Measurement" classes and methods, which can be accessed from AppleScript using ASObjC code. They offer many more categories and units, although it's usually easier, faster, and more efficient (and occasionally more accurate!) to look up the conversion formulae on Wikipedia and write the math directly into the scripts, as above.
 
<langsyntaxhighlight lang="applescript">use AppleScript version "2.5" -- macOS 10.12 (Sierra) or later
use framework "Foundation"
 
Line 543 ⟶ 647:
end convertFromKelvin
 
convertFromKelvin(21)</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">convertKelvins: function [k][
#[
celcius: k - 273.15
Line 555 ⟶ 659:
]
 
print convertKelvins 100</langsyntaxhighlight>
 
{{out}}
 
<pre>[celcius:-173.15 fahrenheit:-279.67 rankine:180.0]</pre>
 
=={{header|Asymptote}}==
<syntaxhighlight lang="Asymptote">void convKelvin(real K) {
write("K = " + string(K));
write("C = " + string(K - 273.15));
write("F = " + string((K - 273.15) * 1.8 + 32.0));
write("R = " + string(K * 1.8));
}
 
convKelvin(0.0);
write("");
convKelvin(21.0);</syntaxhighlight>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">MsgBox, % "Kelvin:`t`t 21.00 K`n"
. "Celsius:`t`t" kelvinToCelsius(21) " C`n"
. "Fahrenheit:`t" kelvinToFahrenheit(21) " F`n"
Line 578 ⟶ 694:
{
return, round(k * 1.8, 2)
}</langsyntaxhighlight>
{{out}}
<pre>Kelvin: 21.00 K
Line 586 ⟶ 702:
 
=={{header|AutoIt}}==
<langsyntaxhighlight AutoItlang="autoit">; ### USAGE - TESTING PURPOSES ONLY
 
Local Const $_KELVIN = 21
Line 605 ⟶ 721:
Return Round($degrees * 1.8, 2)
EndSelect
EndFunc ;==> Kelvin</langsyntaxhighlight>
{{out}}
<pre>Kelvin: 21°
Line 614 ⟶ 730:
=={{header|AWK}}==
"Interactive" version, reading from stdin only:
<langsyntaxhighlight AWKlang="awk"># syntax: AWK -f TEMPERATURE_CONVERSION.AWK
BEGIN {
while (1) {
Line 632 ⟶ 748:
}
exit(0)
}</langsyntaxhighlight>
 
"Regular" version, reading from input-file(s). <br>
Line 638 ⟶ 754:
 
{{works with|gawk}} BEGINFILE is a gawk-extension
<langsyntaxhighlight AWKlang="awk"># usage: gawk -f temperature_conversion.awk input.txt -
 
BEGIN { print("# Temperature conversion\n") }
Line 657 ⟶ 773:
 
END { print("# Bye.") }
</syntaxhighlight>
</lang>
 
{{out|Input}} the numeric value of the first word in each line is used as input for the conversion
Line 730 ⟶ 846:
 
=={{header|BASIC}}==
<syntaxhighlight lang="basic">10 REM TRANSLATION OF AWK VERSION
 
<lang basic>
10 REM TRANSLATION OF AWK VERSION
20 INPUT "KELVIN DEGREES",K
30 IF K <= 0 THEN END: REM A VALUE OF ZERO OR LESS WILL END PROGRAM
Line 742 ⟶ 856:
90 PRINT F; " DEGREES FAHRENHEIT"
100 PRINT R; " DEGREES RANKINE"
110 GOTO 20</syntaxhighlight>
 
</lang>
==={{header|Applesoft BASIC}}===
The [[#Chipmunk Basic|Chipmunk_Basic]] solution works without any changes.
 
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">do
print "Kelvin degrees (>=0): ";
input K
until K>=0
 
print "K = " + string(K)
print "C = " + string(K - 273.15)
print "F = " + string(K * 1.8 - 459.67)
print "R = " + string(K * 1.8)</syntaxhighlight>
 
==={{header|BBC BASIC}}===
<syntaxhighlight lang="bbcbasic">REPEAT
INPUT "Kelvin degrees (>=0): " K
UNTIL K>=0
@%=&20208
PRINT '"K = " K
PRINT "C = " K - 273.15
PRINT "F = " K * 1.8 - 459.67
PRINT "R = " K * 1.8
END</syntaxhighlight>
{{out}}
<pre>Kelvin degrees (>=0): 21
 
K = 21.00
C = -252.15
F = -421.87
R = 37.80</pre>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{works with|QBasic}}
{{works with|QuickBasic}}
{{works with|Applesoft BASIC}}
{{works with|BASICA}}
{{works with|GW-BASIC}}
{{works with|IS-BASIC}}
{{works with|Minimal BASIC}}
{{works with|MSX BASIC|any}}
{{works with|Run BASIC}}
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
<syntaxhighlight lang="qbasic">10 CLS : REM 10 HOME for Applesoft BASIC : DELETE for Minimal BASIC
20 PRINT "Kelvin Degrees ";
30 INPUT K
40 IF K <= 0 THEN 130
50 LET C = K-273.15
60 LET F = K*1.8-459.67
70 LET R = K*1.8
80 PRINT K;" Kelvin is equivalent to"
90 PRINT C;" Degrees Celsius"
100 PRINT F;" Degrees Fahrenheit"
110 PRINT R;" Degrees Rankine"
120 GOTO 20
130 END</syntaxhighlight>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Sub convKelvin(temp As Double)
Dim f As String = "####.##"
Print Using f; temp;
Print " degrees Kelvin"
Print Using f; temp - 273.15;
Print " degrees Celsius"
Print Using f; (temp - 273.15) * 1.8 + 32.0;
Print " degrees Fahreneit"
Print Using f; (temp - 273.15) * 1.8 + 32.0 + 459.67;
Print " degrees Rankine"
End Sub
 
convKelvin(0.0)
Print
convKelvin(21.0)
Print
Print "Press any key to quit"
Sleep</syntaxhighlight>
{{out}}
<pre> 0.00 degrees Kelvin
-273.15 degrees Celsius
-459.67 degrees Fahreneit
0.00 degrees Rankine
 
21.00 degrees Kelvin
-252.15 degrees Celsius
-421.87 degrees Fahreneit
37.80 degrees Rankine</pre>
 
==={{header|Gambas}}===
<syntaxhighlight lang="gambas">Public Sub Form_Open()
Dim fKelvin As Float
 
fKelvin = InputBox("Enter a Kelvin value", "Kelvin converter")
 
Print "Kelvin =\t" & Format(Str(fKelvin), "#.00")
Print "Celsius =\t" & Format(Str(fKelvin - 273.15), "#.00")
Print "Fahrenheit =\t" & Format(Str(fKelvin * 1.8 - 459.67), "#.00")
Print "Rankine =\t" & Format(Str(fKelvin * 1.8), "#.00")
 
End</syntaxhighlight>
{{out}}
<pre>Kelvin = 21.00
Celsius = -252.15
Fahrenheit = -421.87
Rankine = 37.80</pre>
 
==={{header|GW-BASIC}}===
The [[#Chipmunk Basic|Chipmunk_Basic]] solution works without any changes.
 
==={{header|IS-BASIC}}===
<langsyntaxhighlight ISlang="is-BASICbasic">100 INPUT PROMPT "Kelvin degrees: ":K
110 PRINT K;TAB(10);"Kelvin is equivalent to"
120 PRINT K-273.15;TAB(10);"Degrees Celsius"
130 PRINT K*1.8-459.67;TAB(10);"Degrees Fahrenheit"
140 PRINT K*1.8;TAB(10);"Degrees Rankine"</langsyntaxhighlight>
 
==={{header|Liberty BASIC}}===
<syntaxhighlight lang="liberty basic">Do
Input "Kelvin degrees (>=0): ";K
Loop Until (K >= 0)
 
Print "K = ";K
Print "C = ";(K - 273.15)
Print "F = ";(K * 1.8 - 459.67)
Print "R = ";(K * 1.8)
End</syntaxhighlight>
{{out}}
<pre>Kelvin degrees (>=0): 21
K = 21
C = -252.15
F = -421.87
R = 37.8</pre>
 
==={{header|Minimal BASIC}}===
The [[#Chipmunk Basic|Chipmunk_Basic]] solution works without any changes.
 
==={{header|MSX Basic}}===
The [[#Chipmunk Basic|Chipmunk_Basic]] solution works without any changes.
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">Procedure.d Kelvin2Celsius(tK.d) : ProcedureReturn tK-273.15 : EndProcedure
Procedure.d Kelvin2Fahrenheit(tK.d) : ProcedureReturn tK*1.8-459.67 : EndProcedure
Procedure.d Kelvin2Rankine(tK.d) : ProcedureReturn tK*1.8 : EndProcedure
 
OpenConsole()
Repeat
Print("Temperatur Kelvin? ") : Kelvin.d = ValD(Input())
PrintN("Conversion:")
PrintN(#TAB$+"Celsius "+#TAB$+RSet(StrD(Kelvin2Celsius(Kelvin),2),8,Chr(32)))
PrintN(#TAB$+"Fahrenheit"+#TAB$+RSet(StrD(Kelvin2Fahrenheit(Kelvin),2),8,Chr(32)))
PrintN(#TAB$+"Rankine "+#TAB$+RSet(StrD(Kelvin2Rankine(Kelvin),2),8,Chr(32)))
PrintN("ESC = End.")
Repeat
k$=Inkey() : Delay(50) : If RawKey()=#ESC : End : EndIf
Until RawKey()
ForEver</syntaxhighlight>
<pre>Temperatur Kelvin? 21
Conversion:
Celsius -252.15
Fahrenheit -421.87
Rankine 37.80
ESC = End.</pre>
 
==={{header|QB64}}===
<syntaxhighlight lang="qb64">Dim As Single Celsius, Kelvin, Fahrenheit, Rankine
 
Kelvin = -300
While Kelvin = -300
Input "Please type Kelvin temperature...or -300 to quit ", Kelvin
If Kelvin = -300 Then
End
Else
 
Celsius = Kelvin - 273.15
Fahrenheit = ((9 / 5) * Celsius) + 32
Rankine = Fahrenheit + 459.67
End If
Print " Celsius", "Fahrenheit", "Kelvin", "Rankine "
Print Celsius, Fahrenheit, Kelvin, Rankine
Kelvin = -300
Wend</syntaxhighlight>
 
==={{header|QBasic}}===
<syntaxhighlight lang="qbasic">DO
INPUT "Kelvin degrees (>=0): ", K
LOOP UNTIL K >= 0
 
PRINT "K = " + STR$(K)
PRINT "C = " + STR$(K - 273.15)
PRINT "F = " + STR$(K * 1.8 - 459.67)
PRINT "R = " + STR$(K * 1.8)
END</syntaxhighlight>
 
==={{header|Quite BASIC}}===
<syntaxhighlight lang="qbasic">10 PRINT "Kelvin Degrees ";
20 INPUT ""; K
30 IF K <= 0 THEN END
40 LET C = K-273.15
50 LET F = K*1.8-459.67
60 LET R = K*1.8
70 PRINT K;" Kelvin is equivalent to"
80 PRINT C;" Degrees Celsius"
90 PRINT F;" Degrees Fahrenheit"
100 PRINT R;" Degrees Rankine"
110 GOTO 10</syntaxhighlight>
 
==={{header|Sinclair ZX81 BASIC}}===
<langsyntaxhighlight lang="basic">10 PRINT "ENTER A TEMPERATURE IN KELVINS"
20 INPUT K
30 PRINT K;" KELVINS ="
40 PRINT K-273.15;" DEGREES CELSIUS"
50 PRINT K*1.8-459.67;" DEGREES FAHRENHEIT"
60 PRINT K*1.8;" DEGREES RANKINE"</langsyntaxhighlight>
 
==={{Header|Tiny BASIC}}===
<langsyntaxhighlight lang="tiny basic">
PRINT "Temperature in Kelvin?"
INPUT K
Line 769 ⟶ 1,084:
PRINT C," Celsius"
PRINT F," Fahrenheit"
PRINT R," Rankine"</langsyntaxhighlight>
 
==={{header|BASIC256True BASIC}}===
{{works with|QBasic}}
<lang basic256>
<syntaxhighlight lang="qbasic">DO
do
print PRINT "Kelvin degrees (>=0): ";
input INPUT K
LOOP untilUNTIL K >= 0
 
printPRINT "K = "; + stringSTR$(K)
printPRINT "C = "; + stringSTR$(K - 273.15)
printPRINT "F = "; + stringSTR$(K * 1.8 - 459.67)
printPRINT "R = "; + stringSTR$(K * 1.8)
END</syntaxhighlight>
</lang>
 
==={{header|BBC BASICXBasic}}===
{{works with|Windows XBasic}}
<lang bbcbasic>
<syntaxhighlight lang="qbasic">PROGRAM "Temperature conversion"
REPEAT
VERSION "0.001"
INPUT "Kelvin degrees (>=0): " K
UNTIL K>=0
@%=&20208
PRINT '"K = " K
PRINT "C = " K - 273.15
PRINT "F = " K * 1.8 - 459.67
PRINT "R = " K * 1.8
END
</lang>
{{out}}
<pre>
Kelvin degrees (>=0): 21
 
DECLARE FUNCTION Entry()
K = 21.00
 
C = -252.15
FUNCTION Entry()
F = -421.87
DO
R = 37.80
D$ = INLINE$("Kelvin degrees (>=0): ")
</pre>
K = SBYTE(D$)
LOOP UNTIL K >= 0
 
PRINT "K = " + STR$(K)
PRINT "C = " + STR$(K - 273.15)
PRINT "F = " + STR$(K * 1.8 - 459.67)
PRINT "R = " + STR$(K * 1.8)
 
END FUNCTION
END PROGRAM</syntaxhighlight>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="yabasic">repeat
input "Kelvin degrees (>=0): " K
until K >= 0
 
print "K = " + str$(K)
print "C = " + str$(K - 273.15)
print "F = " + str$(K * 1.8 - 459.67)
print "R = " + str$(K * 1.8)
end</syntaxhighlight>
 
==={{header|ZX Spectrum Basic}}===
<syntaxhighlight lang="zxbasic">10 REM Translation of traditional basic version
20 INPUT "Kelvin Degrees? ";k
30 IF k <= 0 THEN STOP: REM A value of zero or less will end program
40 LET c = k - 273.15
50 LET f = k * 1.8 - 459.67
60 LET r = k * 1.8
70 PRINT k; " Kelvin is equivalent to"
80 PRINT c; " Degrees Celsius"
90 PRINT f; " Degrees Fahrenheit"
100 PRINT r; " Degrees Rankine"
110 GO TO 20</syntaxhighlight>
 
=={{header|Befunge}}==
The temperature to convert is read from stdin. Befunge has no support for real numbers, though, so reading and writing of decimal values is done with character I/O. For the same reason, the temperature calculations use integer arithmetic to emulate fixed point. The first two lines handle the input; the second line performs the conversion calculations; and the last three handle the output.
 
<langsyntaxhighlight lang="befunge">0000>0p~>"."-:!#v_2-::0\`\9`+!#v_$1>/\:3`#v_\>\:3 \`#v_v
1#<<^0 /2++g001!<1 \+g00\+*+55\< ^+55\-1< ^*+55\+1<v_
"K"\-+**"!Y]"9:\"C"\--\**"^CIT"/5*9:\"F"\/5*9:\"R"\0\0<v
v/+55\+*86%+55: /+55\+*86%+55: \0/+55+5*-\1*2 p00:`\0:,<
>"."\>:55+% 68*v >:#,_$55+,\:!#@_^
$_^#!:/+55\+< ^\" :"_<g00*95 </langsyntaxhighlight>
 
{{out}}
Line 824 ⟶ 1,162:
 
=={{header|Bracmat}}==
<langsyntaxhighlight lang="bracmat">( ( rational2fixedpoint
= minus fixedpointnumber number decimals
. !arg:(#?number.~<0:~/#?decimals)
Line 869 ⟶ 1,207:
)
& done!
)</langsyntaxhighlight>
{{out}}
<pre>Enter Kelvin temperature:21.00
Line 879 ⟶ 1,217:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 907 ⟶ 1,245:
}
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
 
namespace TemperatureConversion
Line 940 ⟶ 1,278:
}
}
}</langsyntaxhighlight>
 
<pre>
Line 951 ⟶ 1,289:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">
#include <iostream>
#include <iomanip>
Line 1,006 ⟶ 1,344:
}
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,025 ⟶ 1,363:
 
=={{header|Ceylon}}==
<langsyntaxhighlight lang="ceylon">shared void run() {
 
void printKelvinConversions(Float kelvin) {
Line 1,040 ⟶ 1,378:
printKelvinConversions(21.0);
}</langsyntaxhighlight>
 
=={{header|Clojure}}==
{{trans|Common Lisp}}
<langsyntaxhighlight lang="clojure">(defn to-celsius [k]
(- k 273.15))
(defn to-fahrenheit [k]
Line 1,055 ⟶ 1,393:
(format "Celsius: %.2f Fahrenheit: %.2f Rankine: %.2f"
(to-celsius k) (to-fahrenheit k) (to-rankine k))
(format "Error: Non-numeric value entered.")))</langsyntaxhighlight>
 
{{out}}
Line 1,062 ⟶ 1,400:
"Celsius: -252.15 Fahrenheit: -421.87 Rankine: 37.80"
</pre>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">kelvin = proc (k: real) returns (real)
return(k)
end kelvin
 
celsius = proc (k: real) returns (real)
return(k - 273.15)
end celsius
 
rankine = proc (k: real) returns (real)
return(k * 9./5.)
end rankine
 
fahrenheit = proc (k: real) returns (real)
return(rankine(k) - 459.67)
end fahrenheit
 
conv = struct[letter: char, func: proctype (real) returns (real)]
 
convs = sequence[conv]$[
conv${letter: 'K', func: kelvin},
conv${letter: 'C', func: celsius},
conv${letter: 'F', func: fahrenheit},
conv${letter: 'R', func: rankine}
]
 
start_up = proc ()
pi: stream := stream$primary_input()
po: stream := stream$primary_output()
stream$puts(po, "Enter temperature in Kelvin: ")
k: real := real$parse(stream$getl(pi))
for c: conv in sequence[conv]$elements(convs) do
stream$putc(po, c.letter)
stream$puts(po, " ")
stream$putl(po, f_form(c.func(k), 6, 2))
end
end start_up</syntaxhighlight>
{{out}}
<pre>Enter temperature in Kelvin: 21
K 21.00
C -252.15
F -421.87
R 37.80</pre>
 
=={{header|COBOL}}==
{{works with|Visual COBOL}}
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. temp-conversion.
Line 1,100 ⟶ 1,484:
GOBACK
.</langsyntaxhighlight>
 
{{out}}
Line 1,114 ⟶ 1,498:
Three functions define the necessary conversion formulas. A fancy format string is used to print these values.
 
<langsyntaxhighlight lang="lisp">
(defun to-celsius (k)
(- k 273.15))
Line 1,128 ⟶ 1,512:
(to-celsius k) (to-fahrenheit k) (to-rankine k))
(format t "Error: Non-numeric value entered."))))
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,141 ⟶ 1,525:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">double kelvinToCelsius(in double k) pure nothrow @safe {
return k - 273.15;
}
Line 1,173 ⟶ 1,557:
writefln("%2.2f K is below absolute zero", kelvin);
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,184 ⟶ 1,568:
R 37.80
</pre>
 
=={{header|Dart}}==
<syntaxhighlight lang="dart">double kelvinToCelsius(double k) {
return k - 273.15;
}
 
double kelvinToFahrenheit(double k) {
return k * 1.8 - 459.67;
}
 
double kelvinToRankine(double k) {
return k * 1.8;
}
 
void convertKelvin(double kelvin) {
print('K = ${kelvin.toStringAsFixed(2)}');
print('C = ${kelvinToCelsius(kelvin).toStringAsFixed(2)}');
print('F = ${kelvinToFahrenheit(kelvin).toStringAsFixed(2)}');
print('R = ${kelvinToRankine(kelvin).toStringAsFixed(2)}');
print('');
}
 
void main() {
convertKelvin(0.0);
convertKelvin(21.0);
}</syntaxhighlight>
 
=={{header|Delphi}}==
<langsyntaxhighlight lang="delphi">
program Temperature;
 
Line 1,228 ⟶ 1,638:
readln;
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,239 ⟶ 1,649:
=={{header|EasyLang}}==
 
<syntaxhighlight lang="text">k = number input
print k & " °K"
print k - 273.15 & " °C"
print k * 1.8 - 459.67 & " °F"
print k * 1.8 & " °R"</langsyntaxhighlight>
 
=={{header|Elena}}==
ELENA 4.1 :
<langsyntaxhighlight lang="elena">import extensions;
convertKelvinToFahrenheit(x)
Line 1,279 ⟶ 1,689:
console.printLine("Celsius: ", convertKelvinToCelsius(kelvinTemp));
console.readChar()
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,290 ⟶ 1,700:
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule Temperature do
def conversion(t) do
IO.puts "K : #{f(t)}"
Line 1,305 ⟶ 1,715:
end
 
Temperature.task</langsyntaxhighlight>
 
{{out}}
Line 1,319 ⟶ 1,729:
 
=={{header|Erlang}}==
<langsyntaxhighlight lang="erlang">% Implemented by Arjun Sunel
-module(temp_conv).
-export([main/0]).
Line 1,334 ⟶ 1,744:
f(A) ->
(round(A*100))/100 .
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,349 ⟶ 1,759:
 
=={{header|Euphoria}}==
<syntaxhighlight lang="openeuphoria">
<lang OpenEuphoria>
include std/console.e
 
Line 1,357 ⟶ 1,767:
printf(1,"K = %5.2f\nC = %5.2f\nF = %5.2f\nR = %5.2f\n\n",{K,K-273.15,K*1.8-459.67,K*1.8})
end while
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,370 ⟶ 1,780:
 
=={{header|Excel}}==
<syntaxhighlight lang="text">A1 : Kelvin
B1 : Celsius
C1 : Fahrenheit
Line 1,378 ⟶ 1,788:
C2 : =K*1.8-459.67
D2 : =K*1.8
Input in A1 </langsyntaxhighlight>
{{Out}}
<pre>
Line 1,395 ⟶ 1,805:
 
{{Works with|Office 365 betas 2021}}
<langsyntaxhighlight lang="lisp">FROMKELVIN
=LAMBDA(toUnit,
LAMBDA(n,
Line 1,410 ⟶ 1,820:
)
)
)</langsyntaxhighlight>
 
The example below generates the spaced list of test values on the left from the expression ENUMFROMTHENTO(240)(250)(390),
applying the following custom function:
<langsyntaxhighlight lang="lisp">ENUMFROMTHENTO
=LAMBDA(a,
LAMBDA(b,
Line 1,428 ⟶ 1,838:
)
)
)</langsyntaxhighlight>
 
The four columns on the right of the output read their target format from the label cell at the top of each column.
Line 1,567 ⟶ 1,977:
 
=={{header|Ezhil}}==
<syntaxhighlight lang="ezhil">
<lang Ezhil>
# convert from Kelvin
நிரல்பாகம் கெல்வின்_இருந்து_மாற்று( k )
Line 1,576 ⟶ 1,986:
கெல்வின்_இருந்து_மாற்று( 273 ) #freezing pt of water
கெல்வின்_இருந்து_மாற்று( 30 + 273 ) #room temperature in Summer
</syntaxhighlight>
</lang>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Define units of measure
[<Measure>] type k
Line 1,596 ⟶ 2,006:
printfn "%A Kelvin is %A Fahrenheit" K (kelvinToFahrenheit K)
printfn "%A Kelvin is %A Rankine" K (kelvinToRankine K)
</syntaxhighlight>
</lang>
 
=={{header|Factor}}==
<syntaxhighlight lang="text">USING: combinators formatting kernel math ;
IN: rosetta-code.temperature
 
Line 1,610 ⟶ 2,020:
"K %.2f\nC %.2f\nF %.2f\nR %.2f\n" printf ;
21 convert</langsyntaxhighlight>
{{out}}
<pre>
Line 1,620 ⟶ 2,030:
 
=={{header|FOCAL}}==
<langsyntaxhighlight lang="focal">01.10 ASK "TEMPERATURE IN KELVIN", K
01.20 TYPE "K ", %6.02, K, !
01.30 TYPE "C ", %6.02, K - 273.15, !
01.40 TYPE "F ", %6.02, K * 1.8 - 459.67, !
01.50 TYPE "R ", %6.02, K * 1.8, !</langsyntaxhighlight>
{{out}}
<pre>TEMPERATURE IN KELVIN:373.15
Line 1,634 ⟶ 2,044:
=={{header|Forth}}==
{{works with|GNU Forth}} for the command line handling
<langsyntaxhighlight lang="forth">: k>°C ( F: kelvin -- celsius ) 273.15e0 f- ;
: k>°R ( F: kelvin -- rankine ) 1.8e0 f* ;
: °R>°F ( F: rankine -- fahrenheit ) 459.67e0 f- ;
Line 1,646 ⟶ 2,056:
then ;
 
main bye</langsyntaxhighlight>
{{out}}
<pre>&gt; gforthamd64 rosetta_temp_conv.fs 21
Line 1,656 ⟶ 2,066:
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<langsyntaxhighlight lang="fortran">Program Temperature
implicit none
Line 1,681 ⟶ 2,091:
 
end subroutine
end program</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<lang freebasic>' FB 1.05.0 Win64
 
Sub convKelvin(temp As Double)
Dim f As String = "####.##"
Print Using f; temp;
Print " degrees Kelvin"
Print Using f; temp - 273.15;
Print " degrees Celsius"
Print Using f; (temp - 273.15) * 1.8 + 32.0;
Print " degrees Fahreneit"
Print Using f; (temp - 273.15) * 1.8 + 32.0 + 459.67;
Print " degrees Rankine"
End Sub
 
convKelvin(0.0)
Print
convKelvin(21.0)
Print
Print "Press any key to quit"
Sleep</lang>
 
{{out}}
<pre>
0.00 degrees Kelvin
-273.15 degrees Celsius
-459.67 degrees Fahreneit
0.00 degrees Rankine
 
21.00 degrees Kelvin
-252.15 degrees Celsius
-421.87 degrees Fahreneit
37.80 degrees Rankine
</pre>
 
=={{header|Gambas}}==
<lang gambas>Public Sub Form_Open()
Dim fKelvin As Float
 
fKelvin = InputBox("Enter a Kelvin value", "Kelvin converter")
 
Print "Kelvin =\t" & Format(Str(fKelvin), "#.00")
Print "Celsius =\t" & Format(Str(fKelvin - 273.15), "#.00")
Print "Fahrenheit =\t" & Format(Str(fKelvin * 1.8 - 459.67), "#.00")
Print "Rankine =\t" & Format(Str(fKelvin * 1.8), "#.00")
 
End</lang>
Output:
<pre>
Kelvin = 21.00
Celsius = -252.15
Fahrenheit = -421.87
Rankine = 37.80
</pre>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,765 ⟶ 2,120:
fmt.Printf("F %.2f\n", k*9/5-459.67)
fmt.Printf("R %.2f\n", k*9/5)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,776 ⟶ 2,131:
 
=={{header|Groovy}}==
<syntaxhighlight lang="groovy">
<lang Groovy>
class Convert{
static void main(String[] args){
Line 1,789 ⟶ 2,144:
static def k_to_r(def k=21.0){return k*1.8;}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,800 ⟶ 2,155:
=={{header|Haskell}}==
 
<langsyntaxhighlight lang="haskell">import System.Exit (die)
import Control.Monad (mapM_)
 
Line 1,815 ⟶ 2,170:
where labels = ["kelvin: ", "celcius: ", "farenheit: ", "rankine: "]
conversions = [id, subtract 273, subtract 459.67 . (1.8 *), (*1.8)]
nums = (show . ($n)) <$> conversions</langsyntaxhighlight>
 
Or with properly managed exceptions:
 
<langsyntaxhighlight lang="haskell">{-# LANGUAGE LambdaCase #-}
 
import System.Exit (die)
Line 1,842 ⟶ 2,197:
t <- liftIO getLine >>= tryRead "Could not read temp"
tryAssert "Temp cannot be negative" (t>=0)
return t</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
 
The following program works in both languages:
<langsyntaxhighlight lang="unicon">procedure main(A)
k := A[1] | 21.00
write("K ",k)
Line 1,853 ⟶ 2,208:
write("R ",r := k*(9.0/5.0))
write("F ",r - 459.67)
end</langsyntaxhighlight>
 
Sample runs:
Line 1,870 ⟶ 2,225:
->
</pre>
 
=={{header|Insitux}}==
<syntaxhighlight lang="insitux">(var K->C (+ -273.15))
(var K->R (* 1.8))
(var K->F (comp K->R (+ -459.67)))
 
(function kelvin-conversions K
(let [C R F] ((juxt K->C K->R K->F) K)
[C R F] (map @(round 2) [C R F]))
(print K " K / " C " °C / " R " °R / " F " °F"))
 
(kelvin-conversions 21.0)
;prints "21 K / -252.15 °C / 37.8 °R / -421.87 °F"</syntaxhighlight>
 
=={{header|J}}==
'''Solution''':<langsyntaxhighlight lang="j"> NB. Temp conversions are all linear polynomials
K2K =: 0 1 NB. K = (1 *k) + 0
K2C =: _273 1 NB. C = (1 *k) - 273
Line 1,882 ⟶ 2,250:
NB. numeric matrix J programs would manipulate
NB. directly.
k2KCFR =: (K2K , K2C , K2F ,: K2R) p./ ]</langsyntaxhighlight>
{{out|Example}}
<langsyntaxhighlight lang="j"> NB. Format matrix for printing & tag each
NB. temp with scale, for human legibility
fmt =: [: (;:inv"1) 0 _1 |: 'KCFR' ;"0 1"_1 '0.2' 8!:0 ]
Line 1,905 ⟶ 2,273:
C -252.00 -173.00 27.00
F -421.87 -279.67 80.33
R 37.80 180.00 540.00</langsyntaxhighlight>
'''Notes''': The approach is founded on polynomials, one for each conversion (e.g. <tt>Fahrenheit = 1.8*x - 459.67</tt> where <tt>x</tt> is measured in degrees Kelvin), and all polynomials are evaluated simultaneously using the built-in <code>p.</code>. Through some code decorations (specifically the <code>/</code> in <code>p./</code> the <code>"0 1"_1</code> and the <code>0 _1 |:</code>), we permit our function to convert arrays of temperatures of arbitrarily high dimension (a single temp, lists of temps, tables of temps, cubes of temps, etc).
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">public class TemperatureConversion {
public static void main(String args[]) {
if (args.length == 1) {
Line 1,939 ⟶ 2,307:
return k * 1.8;
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,954 ⟶ 2,322:
 
===ES5===
<langsyntaxhighlight lang="javascript">var k2c = k => k - 273.15
var k2r = k => k * 1.8
var k2f = k => k2r(k) - 459.67
Line 1,967 ⟶ 2,335:
kCnv(21)
kCnv(295)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,978 ⟶ 2,346:
Deriving '''kelvinTranslations()''' from a more general '''heatBabel()''' function.
 
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 2,016 ⟶ 2,384:
 
})();
</syntaxhighlight>
</lang>
 
{{Out}}
Line 2,026 ⟶ 2,394:
 
=={{header|jq}}==
The hard part here is defining round/1 generically.<langsyntaxhighlight lang="jq">
# round(keep) takes as input any jq (i.e. JSON) number and emits a string.
# "keep" is the desired maximum number of numerals after the decimal point,
Line 2,063 ⟶ 2,431:
end;
 
cfr</langsyntaxhighlight>
'''Example'''
<langsyntaxhighlight lang="sh"> $ jq -M -r -f Temperature_conversion.jq
21
Kelvin: 21
Line 2,073 ⟶ 2,441:
-1
jq: error: cfr: -1 is an invalid temperature in degrees Kelvin</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">cfr(k) = print("Kelvin: $k, ",
"Celsius: $(round(k-273.15,2)), ",
"Fahrenheit: $(round(k*1.8-459.67,2)), ",
"Rankine: $(round(k*1.8,2))")</langsyntaxhighlight>
<pre>julia> cfr(21)
Kelvin: 21, Celsius: -252.15, Fahrenheit: -421.87, Rankine: 37.8</pre>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.2
 
class Kelvin(val degrees: Double) {
Line 2,104 ⟶ 2,472:
println("F ${f.format(k.toFahreneit())}\n")
println("R ${f.format(k.toRankine())}")
}</langsyntaxhighlight>
 
{{out}}
Line 2,120 ⟶ 2,488:
 
=={{header|Lambdatalk}}==
<langsyntaxhighlight lang="scheme">
{def to-celsius {lambda {:k} {- :k 273.15}}}
-> to-celsius
Line 2,147 ⟶ 2,515:
-421.87 farenheit
37.8 rankine
</syntaxhighlight>
</lang>
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">define tempconverter(temp, kind) => {
 
local(
Line 2,200 ⟶ 2,568:
tempconverter(37.80, 'r')
'<br />'
tempconverter(69.80, 'f')</langsyntaxhighlight>
<pre>K = 21.00 C = -252.15 R = 37.80 F = -421.87
K = 294.15 C = 21.00 R = 529.47 F = 69.80
Line 2,208 ⟶ 2,576:
 
=={{header|LIL}}==
<langsyntaxhighlight lang="tcl"># Temperature conversion, in LIL
func kToc k {expr $k - 273.15}
func kTor k {expr $k / 5.0 * 9.0}
Line 2,219 ⟶ 2,587:
print "Fahrenheit: [kTof $k]"
print "Rankine: [kTor $k]"
}</langsyntaxhighlight>
 
{{out}}
Line 2,230 ⟶ 2,598:
 
=={{header|LiveCode}}==
<langsyntaxhighlight LiveCodelang="livecode">function convertDegrees k
put k/5 * 9 into r
put k - 273.15 into c
put r - 459.67 into f
return k,r,c,f
end convertDegrees</langsyntaxhighlight>
Example<langsyntaxhighlight LiveCodelang="livecode">put convertDegrees(21.00) into tTemp
put item 1 of tTemp into temperature["Kelvin"]
put item 2 of tTemp into temperature["Rankine"]
Line 2,244 ⟶ 2,612:
put temperature
 
-- Celsius:-252.15,Fahrenheit:-421.87,Kelvin:21.00,Rankine:37.8</langsyntaxhighlight>
 
=={{header|Lua}}==
 
<langsyntaxhighlight lang="lua">function convert_temp(k)
local c = k - 273.15
local r = k * 1.8
Line 2,260 ⟶ 2,628:
Rankine: %.2f °R
Fahrenheit: %.2f °F
]],convert_temp(21.0)))</langsyntaxhighlight>
 
=={{header|Maple}}==
<langsyntaxhighlight lang="maple">tempConvert := proc(k)
seq(printf("%c: %.2f\n", StringTools[UpperCase](substring(i, 1)), convert(k, temperature, kelvin, i)), i in [kelvin, Celsius, Fahrenheit, Rankine]);
return NULL;
end proc:
 
tempConvert(21);</langsyntaxhighlight>
{{out}}
<pre>
Line 2,278 ⟶ 2,646:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">tempConvert[t_] := # -> Thread@UnitConvert[#,{"DegreesFahrenheit", "DegreesCelsius", "DegreesRankine"}]&@Quantity[N@t, "Kelvins"]
tempConvert[21]</langsyntaxhighlight>
{{out}}
<pre>21.K -> {-421.87°F,-252.15°C,37.8°R}</pre>
Line 2,285 ⟶ 2,653:
=={{header|min}}==
{{works with|min|0.19.3}}
<langsyntaxhighlight lang="min">(
((float) (273.15 -) (9 5 / * 459.67 -) (9 5 / *)) cleave
() 'cons 4 times "K $1\nC $2\nF $3\nR $4" swap % puts!
) :convert
 
21 convert</langsyntaxhighlight>
{{out}}
<pre>
Line 2,300 ⟶ 2,668:
 
=={{header|MiniScript}}==
<langsyntaxhighlight MiniScriptlang="miniscript">fromKelvin = function(temp)
print temp + " degrees in Kelvin is:"
Celsius = temp - 273.15
Line 2,311 ⟶ 2,679:
 
temp = input("Enter a temperature in Kelvin: ")
fromKelvin temp.val</langsyntaxhighlight>
{{out}}
<pre>
Line 2,328 ⟶ 2,696:
 
=={{header|MiniZinc}}==
<langsyntaxhighlight MiniZinclang="minizinc">float: kelvin;
 
var float: celsius;
Line 2,339 ⟶ 2,707:
solve satisfy;
 
output ["K \(kelvin)\n", "C \(celsius)\n", "F \(fahrenheit)\n", "R \(rankine)\n"];</langsyntaxhighlight>
{{out}}<pre>
Compiling temperature.mzn, additional arguments kelvin=1000;
Line 2,352 ⟶ 2,720:
 
=={{header|МК-61/52}}==
<langsyntaxhighlight lang="mk61">П7 0 , 8 * П8 ИП7 9 * 5
/ 3 2 + П9 ИП7 2 7 3 ,
1 5 + П4 С/П П8 1 , 8 /
БП 00 П9 3 2 - 5 * 9 /
БП 00 П4 2 7 3 , 1 5 -
БП 00</langsyntaxhighlight>
 
''Instruction:''
Line 2,382 ⟶ 2,750:
==={{header|mLite}}===
Temperature in Kelvin given on command line.
<langsyntaxhighlight lang="ocaml">fun KtoC n = n - 273.15;
fun KtoF n = n * 1.8 - 459.67;
fun KtoR n = n * 1.8;
Line 2,398 ⟶ 2,766:
print "Rankine: "; println ` KtoR K
end
</syntaxhighlight>
</lang>
 
=={{header|Nanoquery}}==
{{trans|Python}}
<langsyntaxhighlight Nanoquerylang="nanoquery">% while true
... print "K ? "
... k = float(input())
Line 2,412 ⟶ 2,780:
K ? 222.2
222.200 Kelvin = -50.9500 Celsius = -59.7100 Fahrenheit = 399.960 Rankine degrees.
K ? </langsyntaxhighlight>
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols
 
Line 2,600 ⟶ 2,968:
 
return
</syntaxhighlight>
</lang>
{{out}}
<pre pre style="height: 40ex; overflow: scroll">
Line 2,665 ⟶ 3,033:
 
=={{header|Never}}==
<syntaxhighlight lang="never">
<lang Never>
func KtoC(k : float) -> float { k - 273.15 }
func KtoF(k : float) -> float { k * 1.8 - 459.67 }
Line 2,682 ⟶ 3,050:
0
}
</syntaxhighlight>
</lang>
{{output}}
<pre>
Line 2,692 ⟶ 3,060:
 
=={{header|NewLISP}}==
<syntaxhighlight lang="newlisp">
<lang NewLISP>
(define (to-celsius k)
(- k 273.15)
Line 2,714 ⟶ 3,082:
)
)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,725 ⟶ 3,093:
=={{header|Nim}}==
{{libheader|strfmt}}
<langsyntaxhighlight lang="nim">import rdstdin, strutils, strfmt
 
while true:
let k = parseFloat readLineFromStdin "K ? "
echo "{:g} Kelvin = {:g} Celsius = {:g} Fahrenheit = {:g} Rankine degrees".fmt(
k, k - 273.15, k * 1.8 - 459.67, k * 1.8)</langsyntaxhighlight>
Sample usage:
<pre>K ? 21.0
Line 2,738 ⟶ 3,106:
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">
class Temperature {
function : Main(args : String[]) ~ Nil {
Line 2,764 ⟶ 3,132:
}
}
</syntaxhighlight>
</lang>
 
<pre>
Line 2,774 ⟶ 3,142:
 
=={{header|Objective-C}}==
<langsyntaxhighlight lang="objc">#import <Foundation/Foundation.h>
 
int main(int argc, const char * argv[])
Line 2,792 ⟶ 3,160:
}
return 0;
}</langsyntaxhighlight>
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">
let print_temp s t =
print_string s;
Line 2,816 ⟶ 3,184:
print_temp "F " (kelvin_to_fahrenheit k);
print_temp "R " (kelvin_to_rankine k);;
</syntaxhighlight>
</lang>
 
Sample session:
Line 2,829 ⟶ 3,197:
 
=={{header|Oforth}}==
<syntaxhighlight lang="oforth">: kelvinToCelsius 273.15 - ;
 
<lang Oforth>: kelvinToCelsius 273.15 - ;
: kelvinToFahrenheit 1.8 * 459.67 - ;
: kelvinToRankine 1.8 * ;
Line 2,837 ⟶ 3,204:
n kelvinToCelsius println
n kelvinToFahrenheit println
n kelvinToRankine println ;</langsyntaxhighlight>
 
{{out}}
Line 2,845 ⟶ 3,212:
-421.87
37.8
</pre>
 
=={{header|ooRexx}}==
{{trans|REXX}}
<syntaxhighlight lang="ooRexx">
/* REXX convert temperatures from/to 58 temperature scakes */
Parse Source src
Parse Var src . how .
 
If how='FUNCTION' Then
If arg(2)='' Then
tlist=arg(1) 'TO' 'K'
Else
tlist=arg(1) 'TO' arg(2)
Else Do
Parse Arg tList /* get the list of pairs */
If arg(1)='?' Then Call help
tList= space(tList) /* elide any and all superfluous blanks.*/
End
Do While tList>'' /* process the list */
Parse Var tList pair ',' tList /* pairs are separated by commas */
Parse Var pair from_to '(' desc /* get spec and description (If any) */
Parse upper Var from_to fromt ' TO' toscal . /* get temperature and target scale */
If toscal=='' Then Do /* no target scale: show all targets */
If how='FUNCTION' Then Do
all=0
toscal='K'
End
Else Do
all=1
toscale='all'
End
End
Else Do
all=0
toscale=scalename(toscal)
End
 
Parse Var fromt fromtemp fromscal
If fromscal='' Then
fromscale='K' /*assume kelvin as per task requirement*/
Else
fromscale=scalename(fromscal)
 
If fromt='' Then Call serr 'No source temperature specified'
If \datatype(fromtemp,'N') Then Call serr 'Source temperature ('fromtemp') not numeric'
If left(fromscale,1)='*' Then Call serr 'Invalid source scale' fromscale
If left(toscale,1)='*' Then Call serr 'Invalid target scale' toscale
 
F=convert2Fahrenheit(fromtemp fromscale) /*convert a temperature --? Fahrenheit.*/
 
If F<-459.67 Then Call serr 'Source temperature below absolute zero'
 
If how<>'FUNCTION' Then Do /* write a header line */
If desc<>'' Then desc='('desc
Say pair /*show original value & scale (for sep)*/
If toscale<>'' Then
Say fromtemp fromscale 'TO' toscale
Else
Say fromtemp fromscale 'TO' all
End
 
Call convert2specific
 
End /* while tlist>'' */
Exit /* stick a fork in it, we're all done. */
 
scaleName:
Parse Arg sn /* abbreviations --> temp. short name. */
snU=translate(sn) /* uppercase version of temperature unit*/
snU=translate(snU,'-eE',"_éÉ") /* translate some accented characters. */
 
If left(snU,7)=='DEGREES' Then /* is this a redundant "degrees" ? */
snU=substr(snU,8)
If left(snU,6)=='DEGREE' Then /* " " " " "degree" ? */
snU=substr(snU,7)
snU=strip(snU) /* elide all leading & trailing blanks */
_= length(snU) /* obtain the length of the snU value */
 
If right(snU,1)=='S' & _>1 Then
snU=left(snU,_-1) /* remove any trailing plural(s) */
 
Select /* get scalename from abbrevuation */
When abbrev('ALL' , snU, 3) Then sn= "ALL"
When abbrev('ABSOLUTE' , snU, 1) Then sn= "ABSOLUTE"
When abbrev('AMONTON' , snU) Then sn= "AMONTON"
When abbrev('BARNDORF' , snU,2) |,
abbrev('BARNSDORF' , snU,2) Then sn= "BARNSDORF"
When abbrev('BEAUMIUR' , snU,3) |,
abbrev('BEAUMUIR' , snU,3) Then sn= "BEAUMUIR"
When abbrev('BENERT' , snU,3) |,
abbrev('BENART' , snU,3) Then sn= "BENART"
When abbrev('BRISSEN' , snU,3) |,
abbrev('BRISSON' , snU,3) Then sn= "BRISSEN"
When abbrev('BURGEN' , snU,3) |,
abbrev('BURGAN' , snU,3) |,
abbrev('BERGAN' , snU,3) |,
abbrev('BERGEN' , snU,3) Then sn= "BERGEN"
When abbrev('CENTIGRADE' , snU) |,
abbrev('CENTRIGRADE' , snU) |, /* 50% misspelled.*/
abbrev('CETIGRADE' , snU) |, /* 50% misspelled.*/
abbrev('CENTINGRADE' , snU) |,
abbrev('CENTESIMAL' , snU) |,
abbrev('CELCIU' , snU) |, /* 82% misspelled.*/
abbrev('CELCIOU' , snU) |, /* 4% misspelled.*/
abbrev('CELCUI' , snU) |, /* 4% misspelled.*/
abbrev('CELSUI' , snU) |, /* 2% misspelled.*/
abbrev('CELCEU' , snU) |, /* 2% misspelled.*/
abbrev('CELCU' , snU) |, /* 2% misspelled.*/
abbrev('CELISU' , snU) |, /* 1% misspelled.*/
abbrev('CELSU' , snU) |, /* 1% misspelled.*/
abbrev('HECTOGRADE' , snU) |,
abbrev('CELSIU' , snU) Then sn= "CELSIUS"
When abbrev('CIMANTO' , snU,2) |,
abbrev('CIMENTO' , snU,2) Then sn= "CIMENTO"
When abbrev('CRUQUIOU' , snU,2) |,
abbrev('CRUQUIO' , snU,2) |,
abbrev('CRUQUIU' , snU,2) Then sn= "CRUQUIU"
When abbrev('DALANCE' , snU,4) |,
abbrev('DALENCE' , snU,4) Then sn= "DALENCE"
When abbrev('DANELLE' , snU,3) |,
abbrev('DANEAL' , snU,3) |,
abbrev('DANIAL' , snU,3) |,
abbrev('DANIELE' , snU,3) |,
abbrev('DANNEL' , snU,3) |,
abbrev('DANYAL' , snU,3) |,
abbrev('DANYEL' , snU,3) |,
abbrev('DANIELL' , snU,3) Then sn= "DANIELL"
When abbrev('DALTON' , snU,3) Then sn= "DALTON"
When abbrev('DELAHIRE' , snU,7) |,
abbrev('LAHIRE' , snU,4) |,
abbrev('HIRE' , snU,2) |,
abbrev('DE-LA-HIRE' , snU,7) Then sn= "DE LA HIRE"
When abbrev('DELAVILLE' , snU,7) |,
abbrev('LAVILLE' , snU,3) |,
abbrev('VILLE' , snU,1) |,
abbrev('VILLA' , snU,1) |,
abbrev('DE-LA-VILLE' , snU,7) Then sn= "DE LA VILLE"
When abbrev('DELISLE' , snU,3) Then sn= "DELISLE"
When abbrev('DELISLE-OLD' , snU,8) |,
abbrev('OLDDELISLE' , snU,6) |,
abbrev('DELISLEOLD' , snU,8) Then sn= "DELISLE OLD"
When abbrev('DELUC' , snU,4) |,
abbrev('LUC' , snU,2) |,
abbrev('DE-LUC' , snU,5) Then sn= "DE LUC"
When abbrev('DELYON' , snU,4) |,
abbrev('LYON' , snU,2) |,
abbrev('DE-LYON' , snU,5) Then sn= "DE LYON"
When abbrev('DEREVILLA' , snU,3) |,
abbrev('DEREVILA' , snU,3) |,
abbrev('REVILLA' , snU,3) |,
abbrev('DE-REVILLA' , snU,4) |,
abbrev('DE-REVILLA' , snU,5) Then sn= "DE REVILLAS"
When abbrev('DEVILLENEUVE' , snU,3) |,
abbrev('DE-VILLENEUVE' , snU,4) Then sn= "DE VILLENEUVE"
When abbrev('DURHAM' , snU,3) |,
abbrev('DERHAM' , snU,4) Then sn= "DERHAM"
When abbrev('OLDDURHAM' , snU,5) |,
abbrev('OLDDERHAM' , snU,6) |,
abbrev('DERHAM-OLD' , snU,4) |,
abbrev('DERHAMOLD' , snU,4) Then sn= "DERHAM OLD"
When abbrev('DE-SUEDE' , snU,4) |,
abbrev('DESUEDE' , snU,4) Then sn= "DE SUEDE"
When abbrev('DU-CREST' , snU,2) |,
abbrev('DUCREST' , snU,2) Then sn= "DU CREST"
When abbrev('EDENBURGH' , snU,2) |,
abbrev('EDINBURGH' , snU,2) Then sn= "EDINBURGH"
When abbrev('EVOLT' , snU,2) |,
abbrev('ELECTRONVOLT' , snU,2) Then sn= "ELECTRON VOLTS"
When abbrev('FARENHEIT' , snU) |, /* 39% misspelled.*/
abbrev('FARENHEIGHT' , snU) |, /* 15% misspelled.*/
abbrev('FARENHITE' , snU) |, /* 6% misspelled.*/
abbrev('FARENHIET' , snU) |, /* 3% misspelled.*/
abbrev('FARHENHEIT' , snU) |, /* 3% misspelled.*/
abbrev('FARINHEIGHT' , snU) |, /* 2% misspelled.*/
abbrev('FARENHIGHT' , snU) |, /* 2% misspelled.*/
abbrev('FAHRENHIET' , snU) |, /* 2% misspelled.*/
abbrev('FERENHEIGHT' , snU) |, /* 2% misspelled.*/
abbrev('FEHRENHEIT' , snU) |, /* 2% misspelled.*/
abbrev('FERENHEIT' , snU) |, /* 2% misspelled.*/
abbrev('FERINHEIGHT' , snU) |, /* 1% misspelled.*/
abbrev('FARIENHEIT' , snU) |, /* 1% misspelled.*/
abbrev('FARINHEIT' , snU) |, /* 1% misspelled.*/
abbrev('FARANHITE' , snU) |, /* 1% misspelled.*/
abbrev('FAHRENHEIT' , snU) Then sn= "FAHRENHEIT"
When abbrev('OLDFAHRENHEIT' , snU,4) |,
abbrev('FAHRENHEIT-OLD' , snU,13) |,
abbrev('FAHRENHEITOLD' , snU,13) Then sn= "FARHENHEIT OLD"
When abbrev('FLORENTINE-LARGE' , snU,12) |,
abbrev('LARGE-FLORENTINE' , snU,7) |,
abbrev('LARGEFLORENTINE' , snU,6) |,
abbrev('FLORENTINELARGE' , snU,12) Then sn= "FLORENTINE LARGE"
When abbrev('FLORENTINE-MAGNUM' , snU,2) |,
abbrev('MAGNUM-FLORENTINE' , snU,3) |,
abbrev('MAGNUMFLORENTINE' , snU,3) |,
abbrev('FLORENTINEMAGNUM' , snU,2) Then sn= "FLORENTINE MAGNUM"
When abbrev('FLORENTINE-SMALL' , snU,13) |,
abbrev('SMALL-FLORENTINE' , snU,7) |,
abbrev('SMALLFLORENTINE' , snU,6) |,
abbrev('FLORENTINESMALL' , snU,13) Then sn= "FLORENTINE SMALL"
When abbrev('FOULER' , snU,2) |,
abbrev('FOWLOR' , snU,2) |,
abbrev('FOWLER' , snU,2) Then sn= "FOWLER"
When abbrev('FRICK' , snU,2) Then sn= "FRICK"
When abbrev('GAS-MARK' , snU,2) |,
abbrev('GASMARK' , snU,2) Then sn= "GAS MARK"
When abbrev('GOUBERT' , snU,2) Then sn= "GOUBERT"
When abbrev('HAIL' , snU,3) |,
abbrev('HALE' , snU,3) Then sn= "HALES"
When abbrev('HANOW' , snU,3) Then sn= "HANOW"
When abbrev('HUCKSBEE' , snU,3) |,
abbrev('HAWKSBEE' , snU,3) |,
abbrev('HAUKSBEE' , snU,3) Then sn= "HAUKSBEE"
When abbrev('JACOBSHOLBORN' , snU,2) |,
abbrev('JACOBS-HOLBORN' , snU,2) Then sn= "JACOBS-HOLBORN"
When abbrev('KALVIN' , snU) |, /* 27% misspelled.*/
abbrev('KERLIN' , snU) |, /* 18% misspelled.*/
abbrev('KEVEN' , snU) |, /* 9% misspelled.*/
abbrev('KELVIN' , snU) Then sn= "KELVIN"
When abbrev('LAYDEN' , snU) |,
abbrev('LEIDEN' , snU) Then sn= "LEIDEN"
When abbrev('NEUTON' , snU) |, /*100% misspelled.*/
abbrev('NEWTON' , snU) Then sn= "NEWTON"
When abbrev('ORTEL' , snU) |,
abbrev('OERTEL' , snU) Then sn= "OERTEL"
When abbrev('PLACK' , snU) |, /*100% misspelled.*/
abbrev('PLANC' , snU) |, /* misspelled.*/
abbrev('PLANK' , snU) |, /* misspelled.*/
abbrev('PLANCK' , snU) Then sn= "PLANCK"
When abbrev('RANKINE' , snU, 1) Then sn= "RANKINE"
When abbrev('REAUMUR' , snU, 2) Then sn= "REAUMUR"
When abbrev('RICKTER' , snU, 3) |,
abbrev('RICHTER' , snU, 3) Then sn= "RICHTER"
When abbrev('RINALDINI' , snU, 3) Then sn= "RINALDINI"
When abbrev('ROEMER' , snU, 3) |,
abbrev('ROMER' , snU, 3) Then sn= "ROMER"
When abbrev('ROSANTHAL' , snU, 3) |,
abbrev('ROSENTHAL' , snU, 3) Then sn= "ROSENTHAL"
When abbrev('RSOL' , snU, 2) |,
abbrev('RSL' , snU, 2) |,
abbrev('ROYALSOCIETYOFLONDON' , snU, 3) |,
abbrev('ROYAL-SOCIETY-OF-LONDON' , snU, 3) Then sn= "ROYAL SOCIETY"
When abbrev('SAGREDO' , snU, 3) Then sn= "SAGREDO"
When abbrev('ST.-PATRICE' , snU, 3) |,
abbrev('ST.PATRICE' , snU, 3) |,
abbrev('SAINTPATRICE' , snU, 3) |,
abbrev('SAINT-PATRICE' , snU, 3) Then sn= "SAINT-PATRICE"
When abbrev('STUFFE' , snU, 3) |,
abbrev('STUFE' , snU, 3) Then sn= "STUFE"
When abbrev('SULTZER' , snU, 2) |,
abbrev('SULZER' , snU, 2) Then sn= "SULZER"
When abbrev('WEDGEWOOD' , snU) |,
abbrev('WEDGWOOD' , snU) Then sn= "WEDGWOOD"
Otherwise
sn='***' sn '***'
End /*Select*/
Return sn
 
convert2Fahrenheit: /*convert N --? ºF temperatures. */
/* [?] fifty-eight temperature scales.*/
Parse Arg n sn
Select
When sn=='ABSOLUTE' Then F= n * 9/5 - 459.67
When sn=='AMONTON' Then F= n * 8.37209 - 399.163
When sn=='BARNSDORF' Then F= n * 6.85714 + 6.85714
When sn=='BEAUMUIR' Then F= n * 2.22951 + 32
When sn=='BENART' Then F= n * 1.43391 + 31.2831
When sn=='BERGEN' Then F=(n + 23.8667) * 15/14
When sn=='BRISSEN' Then F= n * 32/15 + 32
When sn=='CELSIUS' Then F= n * 9/5 + 32 /* C -> Celsius.*/
When sn=='CIMENTO' Then F= n * 2.70677 - 4.54135
When sn=='CRUQUIUS' Then F= n * 0.409266 - 405.992
When sn=='DALENCE' Then F= n * 2.7 + 59
When sn=='DALTON' Then F=rxCalcexp(rxCalclog(373.15/273.15)*n/100)*9*273.15/5-459.67
--When sn=='DALTON' Then F=273.15*rxCalcexp(273.15/273.15,n/100)*1.8-459.67
When sn=='DANIELL' Then F= n * 7.27194 + 55.9994
When sn=='DE LA HIRE' Then F=(n - 3) / 0.549057
When sn=='DE LA VILLE' Then F=(n + 6.48011) / 0.985568
When sn=='DELISLE' Then F=212 - n * 6/5
When sn=='DELISLE OLD' Then F=212 - n * 1.58590197
When sn=='DE LUC' Then F=(n + 14) * 16/7
When sn=='DE LYON' Then F=(n + 17.5) * 64/35
When sn=='DE REVILLAS' Then F=212 - n * 97/80
When sn=='DERHAM' Then F= n / 0.38444386 - 188.578
When sn=='DERHAM OLD' Then F= n * 3 + 4.5
When sn=='DE SUEDE' Then F=(n + 17.6666) * 150/83
When sn=='DE VILLENEUVE' Then F=(n + 23.7037) / 0.740741
When sn=='DU CREST' Then F=(n + 37.9202) / 0.650656
When sn=='EDINBURGH' Then F= n * 4.6546 - 6.40048
When sn=='ELECTRON VOLTS' Then F= n * 20888.1 - 459.67
When sn=='FAHRENHEIT' Then F= n
When sn=='FAHRENHEIT OLD' Then F= n * 20/11 - 89.2727
When sn=='FLORENTINE LARGE' Then F=(n + 7.42857) / 0.857143
When sn=='FLORENTINE MAGNUM' Then F=(n + 73.9736 ) / 1.50659
When sn=='FLORENTINE SMALL' Then F=(n - 1.38571) / 0.378571
When sn=='FOWLER' Then F= n * 0.640321 + 53.7709
When sn=='FRICK' Then F= n * 200/251 + 58.5339
When sn=='GASMARK' Then F= n * 25 + 250
When sn=='GOUBERT' Then F= n * 2 + 32
When sn=='HALES' Then F= n * 1.2 + 32
When sn=='HANOW' Then F= n * 1.06668 - 10.6672
When sn=='HAUKSBEE' Then F= n * 18/25 + 88.16
When sn=='JACOBS-HOLBORN' Then F= n * 18/71 - 53.4366
When sn=='K' Then F= n * 9/5 - 459.67
When sn=='KELVIN' Then F= n * 9/5 - 459.67
When sn=='LEIDEN' Then F= n * 1.8 - 423.4
When sn=='NEWTON' Then F= n * 60/11 + 32
When sn=='OERTEL' Then F= n + n - 32
When sn=='PLANCK' Then F= n * 1.416833e32* 9/5 - 459.67
When sn=='RANKINE' Then F= n - 459.67 /* R -> Rankine.*/
When sn=='REAUMUR' Then F= n * 9/4 + 32
When sn=='RICHTER' Then F= n * 160/73 - 7.45205
When sn=='RINALDINI' Then F= n * 15 + 32
When sn=='ROMER' Then F=(n - 7.5) * 27/4+ 32
When sn=='ROSENTHAL' Then F= n * 45/86 - 453.581
When sn=='ROYAL SOCIETY' Then F=(n -122.82) * -50/69
When sn=='SAGREDO' Then F= n * 0.3798 - 5.98
When sn=='SAINT-PATRICE' Then F= n * 2.62123 + 115.879
When sn=='STUFE' Then F= n * 45 + 257
When sn=='SULZER' Then F= n * 1.14595 + 33.2334
When sn=='THERMOSTAT' Then F= n * 54 + 32
When sn=='WEDGWOOD' Then F= n * 44.7429295 + 516.2
Otherwise Call serr 'invalid temperature scale: '
End /*Select*/
Return F
 
convert2specific: /*convert ºF --? xxx temperatures.*/
 
K=(F+459.67)*5/9 /*compute temperature in kelvin scale. */
a=(1e||(-digits()%2)-digits()%20) /*minimum number for Dalton temperature*/
eV=(F+459.67)/20888.1 /*compute the number of electron volts.*/
 
If ?('ABSOLUTE') Then Call line fn(k) "Absolute"
If ?('AMONTON') Then Call line fn((F+399.163) / 8.37209 ) "Amonton"
If ?('BARNSDORF') Then Call line fn( ( F - 6.85715) / 6.85715 ) "Barnsdorf"
If ?('BEAUMUIR') Then Call line fn( ( F - 32 ) / 2.22951 ) "Beaumuir"
If ?('BENART') Then Call line fn( ( F - 31.2831 ) / 1.43391 ) "Benart"
If ?('BERGEN') Then Call line fn( ( F * 14/15 ) - 23.8667 ) "Bergen"
If ?('BRISSON') Then Call line fn( ( F - 32 ) * 15/32 ) "Brisson"
If ?('CELSIUS') Then Call line fn( ( F - 32 ) * 5/9 ) "Celsius"
If ?('CIMENTO') Then Call line fn( ( F + 4.54135) / 2.70677 ) "Cimento"
If ?('CRUQUIUS') Then Call line fn( ( F + 405.992 ) / 0.409266 ) "Cruquius"
If ?('DALENCE') Then Call line fn( ( F - 59 ) / 2.7 ) "Dalence"
If ?('DALTON') Then Do
If K>a Then Call line fn(100*rxCalclog(k/273.15)/rxCalclog(373.15/273.15) ) "Dalton"
Else Call line right("-infinity ", 60) "Dalton"
End
If ?('DANIELL') Then Call line fn( ( F - 55.9994 ) / 7.27194 ) "Daniell"
If ?('DE LA HIRE') Then Call line fn( F * 0.549057 + 3 ) "De la Hire"
If ?('DE LA VILLE') Then Call line fn( F * 0.985568 - 6.48011 ) "De la Ville"
If ?('DELISLE') Then Call line fn( ( 212 - F ) * 5/6 ) "Delisle"
If ?('DELISLE OLD') Then Call line fn( ( 212 - F ) / 1.58590197 ) "Delisle OLD"
If ?('DE LUC') Then Call line fn( F * 7/16 - 14 ) "De Luc"
If ?('DE LYON') Then Call line fn( F * 35/64 - 17.5 ) "De Lyon"
If ?('DE REVILLAS') Then Call line fn( ( 212 - F ) * 80/97 ) "De Revillas"
If ?('DERHAM') Then Call line fn( F * 0.38444386 + 72.4978 ) "Derham"
If ?('DERHAM OLD') Then Call line fn( ( F - 4.5 ) / 3 ) "Derham OLD"
If ?('DE VILLENEUVE') Then Call line fn( F * 0.740741 - 23.7037 ) "De Villeneuve"
If ?('DE SUEDE') Then Call line fn( F * 83/150 - 17.6666 ) "De Suede"
If ?('DU CREST') Then Call line fn( F * 0.650656 - 37.9202 ) "Du Crest"
If ?('EDINBURGH') Then Call line fn( ( F + 6.40048) / 4.6546 ) "Edinburgh"
If ?('ELECTRON VOLTS') Then Call line fn( eV ) "electron volt"s(eV)
If ?('FAHRENHEIT') Then Call line fn( F ) "Fahrenheit"
If ?('FAHRENHEIT OLD') Then Call line fn( F * 20/11 - 89.2727 ) "Fahrenheit OLD"
If ?('FLORENTINE LARGE') Then Call line fn( F * 0.857143 - 7.42857 ) "Florentine large"
If ?('FLORENTINE MAGNUM') Then Call line fn( F * 1.50659 - 73.9736 ) "Florentine Magnum"
If ?('FLORENTINE SMALL') Then Call line fn( F * 0.378571 + 1.38571 ) "Florentine small"
If ?('FOWLER') Then Call line fn( ( F - 53.7709 ) / 0.640321 ) "Fowler"
If ?('FRICK') Then Call line fn( ( F - 58.5338 ) * 251/200 ) "Frick"
If ?('GAS MARK') Then Call line fn( ( F - 250 ) * 0.04 ) "gas mark"
If ?('GOUBERT') Then Call line fn( ( F + 32 ) * 0.5 ) "Goubert"
If ?('HALES') Then Call line fn( ( F - 32 ) / 1.2 ) "Hales"
If ?('HANOW') Then Call line fn( ( F + 10.6672 ) / 1.06668 ) "Hanow"
If ?('HAUKSBEE') Then Call line fn( ( F - 88.16 ) * 25/18 ) "Hauksbee"
If ?('JACOBS-HOLBORN') Then Call line fn( ( F + 53.4366 ) * 71/18 ) "Jacobs-Holborn"
If ?('KELVIN') Then Call line fn( k ) 'KELVIN'
If ?('LEIDEN') Then Call line fn( F / 1.8 + 235.222 ) "Leiden"
If ?('NEWTON') Then Call line fn( ( F - 32 ) * 11/60 ) "Newton"
If ?('OERTEL') Then Call line fn( ( F + 32 ) * 0.5 ) "Oertel"
If ?('PLANCK') Then Call line fn( ( F + 459.67 ) * 5/9 / 1.416833e32 ) "Planck"
If ?('RANKINE') Then Call line fn( F + 459.67 ) "Rankine"
If ?('REAUMUR') Then Call line fn( ( F - 32 ) * 4/9 ) "Reaumur"
If ?('RICHTER') Then Call line fn( ( F + 7.45205) * 73/160 ) "Richter"
If ?('RINALDINI') Then Call line fn( ( F - 32 ) / 15 ) "Rinaldini"
If ?('ROMER') Then Call line fn( ( F - 32 ) * 4/27 + 7.5 ) "Romer"
If ?('ROSENTHAL') Then Call line fn( ( F + 453.581 ) * 86/45 ) "Rosenthal"
If ?('ROYAL SOCIETY') Then Call line fn( F * -69/50 + 122.82 ) "Royal Society of London"
If ?('SAGREDO') Then Call line fn( ( F + 5.98 ) / 0.3798 ) "Segredo"
If ?('SAINT-PATRICE') Then Call line fn( ( F - 115.879 ) / 2.62123 ) "Saint-Patrice"
If ?('STUFE') Then Call line fn( ( F - 257 ) / 45 ) "Stufe"
If ?('SULZER') Then Call line fn( ( F - 33.2334 ) / 1.14595 ) "Sulzer"
If ?('THERMOSTAT') Then Call line fn( ( F - 32 ) / 54 ) "Thermostat"
If ?('WEDGWOOD') Then Call line fn( ( F - 516.2 ) / 44.7429295 ) "Wedgwood"
Return
 
line:
If how='FUNCTION' & all=0 Then
Exit space(arg(1))
Else
Say arg(1)
Return
 
?:
Return (arg(1)=toscale | all)
 
fn: Procedure Expose how result
showDig=8 /* only show 8 decimal digs. */
number=commas(format(arg(1),,showDig)/1) /* format# 8 digits past . and add commas */
p=pos('.',number) /* find position of the decimal point. */
/* [?] align integers with FP numbers. */
If p==0 Then /* no decimal point .*/
number=number||left('',5+showDig+1)
Else /* ddd.ddd */
number=number||left('',5+showDig-length(number)+p)
Return right(number,max(25,length(number)))/* return the re-formatted argument (#).*/
 
commas: Procedure
Parse Arg u
a=pos('.',u'.')
e=1
If left(u,1)='-' Then e=2
Do j=a-4 To e by -3
u=insert(',',u,j)
End
Return u
 
s:
If arg(1)==1 Then Return arg(3)
Return word(arg(2)'s',1) /*pluralizer.*/
 
serr:
If how='FUNCTION' Then
Exit arg(1)
Else
Say arg(1)
Exit 13
 
help:
Say 'use as command:'
Say 'rexx tcw fromtemp [fromscale] [TO toscale | all],...'
Say 'or as function'
Say 'tcw(fromtemp [fromscale] [TO toscale])'
Exit
::Requires rxmath library
</syntaxhighlight>
 
{{out}}
<pre>
K:\_B\TC>rexx tcwoo 0 C to fa
0 C to fa
0 CELSIUS TO FAHRENHEIT
32 Fahrenheit
K:\_B\TC>rexx tcwoo 0 f
0 f
0 FAHRENHEIT TO all
255.372222 Absolute
47.67782 Amonton
-1 Barnsdorf
-14.3529296 Beaumuir
-21.8166412 Benart
-23.8667 Bergen
-15 Brisson
-17.7777778 Celsius
1.67777462 Cimento
992.000313 Cruquius
-21.8518519 Dalence
-21.5729747 Dalton
-7.70075111 Daniell
3 De la Hire
-6.48011 De la Ville
176.666667 Delisle
133.677872 Delisle OLD
-14 De Luc
-17.5 De Lyon
174.845361 De Revillas
72.4978 Derham
-1.5 Derham OLD
-23.7037 De Villeneuve
-17.6666 De Suede
-37.9202 Du Crest
1.37508701 Edinburgh
0.02200631 electron volts
0 Fahrenheit
-89.2727 Fahrenheit OLD
-7.42857 Florentine large
-73.9736 Florentine Magnum
1.38571 Florentine small
-83.9749126 Fowler
-73.459919 Frick
-10 gas mark
16 Goubert
-26.6666667 Hales
10.000375 Hanow
-122.444444 Hauksbee
210.7777 Jacobs-Holborn
255.372222 KELVIN
235.222 Leiden
-5.86666667 Newton
16 Oertel
1.80241582E-30 Planck
459.67 Rankine
-14.2222222 Reaumur
3.39999781 Richter
-2.13333333 Rinaldini
2.75925926 Romer
866.843689 Rosenthal
122.82 Royal Society of London
15.745129 Segredo
-44.2078719 Saint-Patrice
-5.71111111 Stufe
-29.0007417 Sulzer
-0.59259259 Thermostat
-11.5370184 Wedgwood
</pre>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">f(x)=[x,x-273.15,1.8*x-459.67,1.8*x]</langsyntaxhighlight>
 
=={{header|Pascal}}==
<langsyntaxhighlight Pascallang="pascal">program TemperatureConvert;
 
type
Line 2,904 ⟶ 3,784:
writeln(' ', ConvertTemperature(kelvin, K, F) : 3 : 2, ' in degrees Fahrenheit.');
writeln(' ', ConvertTemperature(kelvin, K, R) : 3 : 2, ' in degrees Rankine.');
end.</langsyntaxhighlight>
 
{{out}}
Line 2,916 ⟶ 3,796:
 
=={{header|Perl}}==
<langsyntaxhighlight Perllang="perl">my %scale = (
Celcius => { factor => 1 , offset => -273.15 },
Rankine => { factor => 1.8, offset => 0 },
Line 2,928 ⟶ 3,808:
foreach (sort keys %scale) {
printf "%12s:%8.2f\n", $_, $kelvin * $scale{$_}{factor} + $scale{$_}{offset};
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,939 ⟶ 3,819:
=={{header|Phix}}==
Modified copy of [[Temperature_conversion#Euphoria|Euphoria]]
<!--<langsyntaxhighlight Phixlang="phix">-->
<span style="color: #004080;">atom</span> <span style="color: #000000;">K</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">prompt_number</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Enter temperature in Kelvin >=0: "</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1e307</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" Kelvin: %5.2f\n Celsius: %5.2f\nFahrenheit: %5.2f\n Rankine: %5.2f\n\n"</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">K</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">K</span><span style="color: #0000FF;">-</span><span style="color: #000000;">273.15</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">K</span><span style="color: #0000FF;">*</span><span style="color: #000000;">1.8</span><span style="color: #0000FF;">-</span><span style="color: #000000;">459.67</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">K</span><span style="color: #0000FF;">*</span><span style="color: #000000;">1.8</span><span style="color: #0000FF;">})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,954 ⟶ 3,834:
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php">
 
while (true) {
Line 2,973 ⟶ 3,853:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Enter a value in kelvin (q to quit): 21
Line 2,985 ⟶ 3,865:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(scl 2)
 
(de convertKelvin (Kelvin)
Line 2,996 ⟶ 3,876:
(tab (-3 8)
(car X)
(format ((cdr X) Kelvin) *Scl) ) ) )</langsyntaxhighlight>
Test:
<syntaxhighlight lang PicoLisp="picolisp">(convertKelvin 21.0)</langsyntaxhighlight>
{{out}}
<pre>K 21.00
Line 3,006 ⟶ 3,886:
 
=={{header|PL/I}}==
<langsyntaxhighlight lang="pli">*process source attributes xref;
/* PL/I **************************************************************
* 15.08.2013 Walter Pachl translated from NetRexx
Line 3,153 ⟶ 4,033:
End;
End;
End;</langsyntaxhighlight>
{{out}}
<pre>
Line 3,169 ⟶ 4,049:
 
=={{header|Plain English}}==
<langsyntaxhighlight lang="plainenglish">To run:
Start up.
Put 21 into a kelvin temperature.
Line 3,202 ⟶ 4,082:
Show the celsius temperature given "C".
Show the fahrenheit temperature given "F".
Show the rankine temperature given "R".</langsyntaxhighlight>
{{out}}
<pre>
Line 3,213 ⟶ 4,093:
=={{header|PowerShell}}==
{{trans|Tcl}}
<langsyntaxhighlight lang="powershell">function temp($k){
try{
$c = $k - 273.15
Line 3,233 ⟶ 4,113:
 
$input=Read-host "Enter a temperature in Kelvin"
temp $input</langsyntaxhighlight>
{{Out}}
<pre>PS> ./TEMPS
Line 3,247 ⟶ 4,127:
===PowerShell Alternate Version===
A more "PowerShelly" way to do it.
<syntaxhighlight lang="powershell">function Convert-Kelvin
<lang PowerShell>
function Convert-Kelvin
{
[CmdletBinding()]
Line 3,274 ⟶ 4,153:
}
}
}</syntaxhighlight>
}
<syntaxhighlight lang="powershell">
</lang>
<lang PowerShell>
21, 100 | Convert-Kelvin
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 3,286 ⟶ 4,164:
100 -173.15 -279.67 180
</pre>
 
=={{header|Prolog}}==
{{works with|GNU Prolog}}
{{works with|SWI Prolog}}
<syntaxhighlight lang="prolog">convKelvin(Temp) :-
Kelvin is Temp,
Celsius is Temp - 273.15,
Fahrenheit is (Temp - 273.15) * 1.8 + 32.0,
Rankine is (Temp - 273.15) * 1.8 + 32.0 + 459.67,
format('~f degrees Kelvin~n', [Kelvin]),
format('~f degrees Celsius~n', [Celsius]),
format('~f degrees Fahrenheit~n', [Fahrenheit]),
format('~f degrees Rankine~n', [Rankine]).
 
test :-
convKelvin(0.0),
nl,
convKelvin(21.0).</syntaxhighlight>
 
=={{header|Pure Data}}==
 
'''temperature.pd'''
<pre>#N canvas 200 200 640 600 10;
<pre>
#N canvas 200 200 640 600 10;
#X floatatom 130 54 8 0 0 2 Kelvin chgk -;
#X obj 130 453 rnd2;
Line 3,355 ⟶ 4,249:
#X connect 24 0 4 0;
#X connect 25 0 26 0;
#X connect 26 0 7 0;</pre>
</pre>
Plugin to round the results to at most 2 digits:
 
'''rnd.pd'''
<pre>#N canvas 880 200 450 300 10;
<pre>
#N canvas 880 200 450 300 10;
#X obj 77 34 inlet;
#X obj 77 113 * 100;
Line 3,377 ⟶ 4,269:
#X connect 4 0 5 0;
#X connect 5 0 6 0;
#X connect 6 0 7 0;</pre>
</pre>
 
=={{header|PureBasic}}==
<lang purebasic>Procedure.d Kelvin2Celsius(tK.d) : ProcedureReturn tK-273.15 : EndProcedure
Procedure.d Kelvin2Fahrenheit(tK.d) : ProcedureReturn tK*1.8-459.67 : EndProcedure
Procedure.d Kelvin2Rankine(tK.d) : ProcedureReturn tK*1.8 : EndProcedure
 
OpenConsole()
Repeat
Print("Temperatur Kelvin? ") : Kelvin.d = ValD(Input())
PrintN("Conversion:")
PrintN(#TAB$+"Celsius "+#TAB$+RSet(StrD(Kelvin2Celsius(Kelvin),2),8,Chr(32)))
PrintN(#TAB$+"Fahrenheit"+#TAB$+RSet(StrD(Kelvin2Fahrenheit(Kelvin),2),8,Chr(32)))
PrintN(#TAB$+"Rankine "+#TAB$+RSet(StrD(Kelvin2Rankine(Kelvin),2),8,Chr(32)))
PrintN("ESC = End.")
Repeat
k$=Inkey() : Delay(50) : If RawKey()=#ESC : End : EndIf
Until RawKey()
ForEver</lang>
<pre>Temperatur Kelvin? 21
Conversion:
Celsius -252.15
Fahrenheit -421.87
Rankine 37.80
ESC = End.</pre>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">>>> while True:
k = float(input('K ? '))
print("%g Kelvin = %g Celsius = %g Fahrenheit = %g Rankine degrees."
Line 3,415 ⟶ 4,282:
K ? 222.2
222.2 Kelvin = -50.95 Celsius = -59.71 Fahrenheit = 399.96 Rankine degrees.
K ? </langsyntaxhighlight>
 
===Python: Universal conversion===
This converts from any one of the units to all the others
<langsyntaxhighlight lang="python">>>> toK = {'C': (lambda c: c + 273.15),
'F': (lambda f: (f + 459.67) / 1.8),
'R': (lambda r: r / 1.8),
Line 3,438 ⟶ 4,305:
<value> <K/R/F/C> ? 399.96 R
222.2 Kelvin = -50.95 Celsius = -59.71 Fahrenheit = 399.96 Rankine degrees.
<value> <K/R/F/C> ? </langsyntaxhighlight>
 
=={{header|Quackery}}==
 
All the conversions.
 
Using the Quackery big number rational arithmetic library <code>bigrat.qky</code>.
 
<langsyntaxhighlight Quackerylang="quackery"> [ $ "bigrat.qky" loadfile ] now!
[ 5 9 v* ] is r->k ( n/d --> n/d )
Line 3,476 ⟶ 4,342:
say " degrees Rankine" cr ] is task ( $ --> )
$ "21.00" task</langsyntaxhighlight>
 
{{out}}
 
<pre>21 Kelvins is equal to
-252.15 degrees Celcius
-421.87 degrees Fahrenheit
37.8 degrees Rankine</pre>
 
 
=={{header|R}}==
<syntaxhighlight lang="r">convert_Kelvin <- function(K){
if (!is.numeric(K))
stop("\n Input has to be numeric")
return(list(
Kelvin = K,
Celsius = K - 273.15,
Fahreneit = K * 1.8 - 459.67,
Rankine = K * 1.8
))
}
convert_Kelvin(21)
</syntaxhighlight>
{{out}}
<pre> $Kelvin
[1] 21
$Celsius
[1] -252.15
$Fahreneit
[1] -421.87
$Rankine
[1] 37.8</pre>
 
=={{header|Racket}}==
Although not exactly the shortest code,
the converter function can turn any temperature into any other
<langsyntaxhighlight Racketlang="racket">#lang racket
(define (converter temp init final)
(define to-k
Line 3,513 ⟶ 4,407:
;Fahrenheit: -421.87
;Rankine: 37.800000000000004
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 3,519 ⟶ 4,413:
 
{{trans|Perl}}
<syntaxhighlight lang="raku" perl6line>my %scale =
Celcius => { factor => 1 , offset => -273.15 },
Rankine => { factor => 1.8, offset => 0 },
Line 3,530 ⟶ 4,424:
for %scale.sort {
printf "%12s: %7.2f\n", .key, $kelvin * .value<factor> + .value<offset>;
}</langsyntaxhighlight>
 
{{out}}
Line 3,542 ⟶ 4,436:
Alternative version that accepts the input in any of the four scales:
 
<syntaxhighlight lang="raku" perl6line>while my $answer = prompt 'Temperature: ' {
my $k = do given $answer {
when s/:i C $// { $_ + 273.15 }
Line 3,554 ⟶ 4,448:
say " { $k * 1.8 - 459.67 }℉";
say " { $k * 1.8 }R";
}</langsyntaxhighlight>
{{out}}
<pre>Temperature: 0
Line 3,602 ⟶ 4,496:
::* comments (annotation notes) allowed within the list
::* aligned output (whole numbers and decimal fractions)
<langsyntaxhighlight lang="rexx">/*REXX program converts temperatures for a number (8) of temperature scales. */
numeric digits 120 /*be able to support some huge numbers.*/
parse arg tList /*get the specified temperature list. */
Line 3,625 ⟶ 4,519:
if \all then do /*is there is a TO ααα scale? */
call name ! /*process the TO abbreviation. */
!= s nsn /*assign the full name to ! */
end /*!: now contains temperature full name*/
call name u /*allow alternate scale (miss)spellings*/
Line 3,718 ⟶ 4,612:
otherwise call serr 'illegal temperature scale:' y
end /*select*/
return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 98.6F to C, &nbsp; -40C, 0 c (water freezes), &nbsp; 37C (body temp), &nbsp; 100 C (water boils), &nbsp; 21 degrees Kelvin, &nbsp; 0 K (outer space?) </tt>}}
<pre>
Line 3,918 ⟶ 4,812:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
k = 21.0 c = 0 r = 0 f = 0
convertTemp(k)
Line 3,930 ⟶ 4,824:
r = k * 1.8
f = r - 459.67
</syntaxhighlight>
</lang>
 
=={{header|RPL}}==
RPL has a built-in conversion feature with a convenient catalog of units.
≪ { "°C" "°F" "°R" } → tempk units
≪ 1 3 '''FOR''' j
tempk "°K" units j GET CONVERT
SWAP →STR SWAP +
'''NEXT'''
≫ ≫ 'K→CFR' STO
 
21 K→CFR
{{out}}
<pre>
3: "-252.15°C"
2: "-421.87°F"
1: "37.8°R"
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">module TempConvert
 
FROM_TEMP_SCALE_TO_K =
Line 3,979 ⟶ 4,890:
end
end</langsyntaxhighlight>
Converts all eight scales to any other scale, by means of method_missing.
 
Usage:
<langsyntaxhighlight lang="ruby">TempConvert.kelvin_to_celsius 100 #=> -173.15
TempConvert.kelvin_to_fahrenheit 100 #=> -279.67
TempConvert.kelvin_to_rankine 100 #=> 180.0
Line 3,993 ⟶ 4,904:
TempConvert.newton_to_celsius 100 #=> 303.03
TempConvert.newton_to_fahrenheit 100 #=> 577.45
# All 64 combinations possible</langsyntaxhighlight>
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">[loop]
input "Kelvin Degrees";kelvin
if kelvin <= 0 then end ' zero or less ends the program
Line 4,003 ⟶ 4,914:
rankine = kelvin * 1.8
print kelvin;" kelvin is equal to ";celcius; " degrees celcius and ";fahrenheit;" degrees fahrenheit and ";rankine; " degrees rankine"
goto [loop]</langsyntaxhighlight>
 
=={{header|Scala}}==
{{libheader|Scala}}
<langsyntaxhighlight Scalalang="scala">object TemperatureConversion extends App {
 
def kelvinToCelsius(k: Double) = k + 273.15
Line 4,033 ⟶ 4,944:
}
} else println("Temperature not given.")
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,043 ⟶ 4,954:
 
=={{header|Rust}}==
<langsyntaxhighlight Rustlang="rust">fn main() -> std::io::Result<()> {
print!("Enter temperature in Kelvin to convert: ");
let mut input = String::new();
Line 4,063 ⟶ 4,974:
 
Ok(())
}</langsyntaxhighlight>
 
=={{header|Scheme}}==
 
<langsyntaxhighlight lang="scheme">
(import (scheme base)
(scheme read)
Line 4,087 ⟶ 4,998:
(display "Fahrenheit: ") (display (kelvin->fahrenheit k)) (newline)
(display "Rankine : ") (display (kelvin->rankine k)) (newline)))
</syntaxhighlight>
</lang>
 
{{out}}
Line 4,098 ⟶ 5,009:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
 
Line 4,120 ⟶ 5,031:
writeln("F: " <& fahrenheit(kelvin) digits 2 lpad 7);
writeln("R: " <& rankine(kelvin) digits 2 lpad 7);
end func;</langsyntaxhighlight>
 
{{out}}
Line 4,133 ⟶ 5,044:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">var scale = Hash(
Celcius => Hash.new(factor => 1 , offset => -273.15 ),
Rankine => Hash.new(factor => 1.8, offset => 0 ),
Line 4,144 ⟶ 5,055:
scale.keys.sort.each { |key|
printf("%12s:%8.2f\n", key, kelvin*scale{key}{:factor} + scale{key}{:offset});
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,155 ⟶ 5,066:
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">
func KtoC(kelvin : Double)->Double{
Line 4,179 ⟶ 5,090:
var r=KtoR(kelvin : k)
print("\(r) Rankine")
</syntaxhighlight>
</lang>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">proc temps {k} {
set c [expr {$k - 273.15}]
set r [expr {$k / 5.0 * 9.0}]
set f [expr {$r - 459.67}]
list $k $c $f $r
}</langsyntaxhighlight>
 
 
Demonstrating:
<langsyntaxhighlight lang="tcl">puts -nonewline "Enter a temperature in K: "
flush stdout
lassign [temps [gets stdin]] k c f r
Line 4,197 ⟶ 5,108:
puts [format "C: %.2f" $c]
puts [format "F: %.2f" $f]
puts [format "R: %.2f" $r]</langsyntaxhighlight>
{{out}}
<pre>
Line 4,209 ⟶ 5,120:
=={{header|UNIX Shell}}==
 
==={{header|Korn Shellksh}}===
<syntaxhighlight lang="ksh">#!/bin/ksh
{{works with|ksh}}
<lang bash>#!/bin/ksh
# Temperature conversion
typesetset -A tt[1]= 0.00 tt[2]=273.15 tt[3]=373.15
for it in "${1..3tt[@]}"
do
((t=tt[i]))echo
echo $i
echo "Kelvin: $t K"
echo "Celsius: $((t-273.15)) C"
echo "Fahrenheit: $((t*18/10-459.67)) F"
echo "Rankine: $((t*18/10)) R"
done</langsyntaxhighlight>
 
==={{header|bash}}===
{{works with|Bourne Again SHell}}
<langsyntaxhighlight lang="bash">#!/bin/bash
# Temperature conversion
tt[1]=0.00; tt[2]=273.15; tt[3]=373.15
Line 4,237 ⟶ 5,146:
echo "Fahrenheit: $(bc<<<"scale=2;$t*18/10-459.67") F"
echo "Rankine: $(bc<<<"scale=2;$t*18/10") R"
done</langsyntaxhighlight>
 
=={{header|Ursa}}==
<langsyntaxhighlight lang="ursa">decl double k
while true
out "Temp. in Kelvin? " console
Line 4,246 ⟶ 5,155:
out "K\t" k endl "C\t" (- k 273.15) endl console
out "F\t" (- (* k 1.8) 459.67) endl "R\t" (* k 1.8) endl endl console
end while</langsyntaxhighlight>
 
=={{header|VBA}}==
 
<syntaxhighlight lang="vb">
<lang vb>
Option Explicit
 
Line 4,275 ⟶ 5,184:
End Select
End Function
</syntaxhighlight>
</lang>
{{out}}
<pre>Input in Kelvin : 21,00
Line 4,284 ⟶ 5,193:
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
<lang vb>
WScript.StdOut.Write "Enter the temperature in Kelvin:"
tmp = WScript.StdIn.ReadLine
Line 4,304 ⟶ 5,213:
rankine = (k-273.15)*1.8+491.67
End Function
</syntaxhighlight>
</lang>
 
{{Out}}
Line 4,317 ⟶ 5,226:
 
=={{header|Visual FoxPro}}==
<langsyntaxhighlight lang="vfp">#DEFINE ABSZC 273.16
#DEFINE ABSZF 459.67
LOCAL k As Double, c As Double, f As Double, r As Double, n As Integer, ;
Line 4,341 ⟶ 5,250:
ENDDO
SET FIXED &cf
SET DECIMALS TO n</langsyntaxhighlight>
{{out}}
<pre>
Line 4,348 ⟶ 5,257:
F: -421.87
R: 37.80
</pre>
 
=={{header|V (Vlang)}}==
Note: round_sig in 0.3 or later
<syntaxhighlight lang="v (vlang)">import math
 
fn main() {
c, f, r := kelvin_to_cfr(21)
println('Celsius: $c˚\nFahrenheit: $f˚\nRankine: $r˚')
}
 
fn kelvin_to_cfr(kelvin f64) (string, string, string) {
celsius := math.round_sig(kelvin - 273.15, 2)
fahrenheit := math.round_sig(kelvin * 1.8 - 459.67, 2)
rankine := math.round_sig(kelvin * 1.8, 2)
return celsius.str(), fahrenheit.str(), rankine.str()
}</syntaxhighlight>
 
{{out}}
<pre>
Celsius: -252.15˚
Fahrenheit: -421.87˚
Rankine: 38.80˚
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
var tempConv = Fn.new { |k|
Line 4,366 ⟶ 5,298:
 
var ks = [0, 21, 100]
for (k in ks) tempConv.call(k)</langsyntaxhighlight>
 
{{out}}
Line 4,387 ⟶ 5,319:
 
=={{header|XLISP}}==
<langsyntaxhighlight lang="xlisp">(DEFUN CONVERT-TEMPERATURE ()
(SETQ *FLONUM-FORMAT* "%.2f")
(DISPLAY "Enter a temperature in Kelvin.")
Line 4,399 ⟶ 5,331:
(DISPLAY `(F = ,(- (* K 1.8) 459.67)))
(NEWLINE)
(DISPLAY `(R = ,(* K 1.8))))</langsyntaxhighlight>
{{out}}
<pre>(CONVERT-TEMPERATURE)
Line 4,410 ⟶ 5,342:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes;
real K, C, F, R;
[ChOut(0, ^K); K:= RlIn(0);
Line 4,419 ⟶ 5,351:
R:= F + 459.67;
ChOut(0, ^R); RlOut(0, R); CrLf(0);
]</langsyntaxhighlight>
{{out}}
<pre>K 21
K 21
C -252.15000
F -421.87000
R 37.80000</pre>
</pre>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">K:=ask(0,"Kelvin: ").toFloat();
println("K %.2f".fmt(K));
println("F %.2f".fmt(K*1.8 - 459.67));
println("C %.2f".fmt(K - 273.15));
println("R %.2f".fmt(K*1.8));</langsyntaxhighlight>
{{out}}
<pre>Kelvin: 373.15
Kelvin: 373.15
K 373.15
F 212.00
C 100.00
R 671.67</pre>
</pre>
 
=={{header|ZX Spectrum Basic}}==
 
<lang zxbasic>10 REM Translation of traditional basic version
20 INPUT "Kelvin Degrees? ";k
30 IF k <= 0 THEN STOP: REM A value of zero or less will end program
40 LET c = k - 273.15
50 LET f = k * 1.8 - 459.67
60 LET r = k * 1.8
70 PRINT k; " Kelvin is equivalent to"
80 PRINT c; " Degrees Celsius"
90 PRINT f; " Degrees Fahrenheit"
100 PRINT r; " Degrees Rankine"
110 GO TO 20</lang>
2,122

edits