--- title: "Temperature Conversion" id: 57284 type: "computer_media" slug: "temperature-conversion-2" url: "http://localhost/computer_media/temperature-conversion-2/" markdown_url: "http://localhost/computer_media/temperature-conversion-2.md" published_at: "2024-10-04T08:05:20+00:00" modified_at: "2026-03-30T21:18:38+00:00" author: "David Anderson" featured_image: url: "http://localhost/wp-content/uploads/2024/10/172_Temp.png" excerpt: "A neat temperature converter that handles Fahrenheit, Celsius, and Kelvin with input validation and a clean display loop — all in under 20 lines of BASIC." category: - name: "Archived Media" slug: "archived-media" taxonomy: "category" url: "http://localhost/category/archived-media/" post_tag: - name: "Downloadable" slug: "downloadable" taxonomy: "post_tag" url: "http://localhost/tag/downloadable/" - name: "TS 1000" slug: "ts1000" taxonomy: "post_tag" url: "http://localhost/tag/ts1000/" model: - name: "Timex/Sinclair 1000" slug: "ts-1000" taxonomy: "model" url: "http://localhost/model/ts-1000/" genre: - name: "Mathematics" slug: "mathematics" taxonomy: "genre" url: "http://localhost/type/mathematics/" media_contents: - id: 56734 title: "Timex Sinclair Public Domain Library Tape 1003" type: "computer_media" url: "http://localhost/computer_media/timex-sinclair-public-domain-library-tape-1003/" media_type: "Program" mediadate: "198x" images: - url: "http://localhost/wp-content/uploads/2024/10/172_Temp.png" media_type_tags: "Mathematics" --- This program converts temperatures between Fahrenheit, Celsius, and Kelvin. The user enters a numeric temperature and selects a unit (F, C, or K); an input validation loop at line 70 rejects anything other than these three letters before proceeding. Two subroutines handle the conversions: lines 200–220 convert Fahrenheit to Celsius using the standard (T−32)×5/9 formula, while lines 300–340 handle both Celsius-to-Fahrenheit and Kelvin-to-Fahrenheit by first normalising Kelvin to Celsius before applying the C×9/5+32 formula. After displaying the result, the program waits for any key press via INPUT A$ before clearing the screen and looping back to the start. *** ## Program Analysis ### Program Structure The program is organised into a main loop and two conversion subroutines: 1. **Lines 10–150**: Main loop — prompts for temperature and unit, dispatches to a subroutine, displays the result, waits for a keypress, then repeats. 2. **Lines 200–220**: Fahrenheit → Celsius subroutine. 3. **Lines 300–340**: Celsius or Kelvin → Fahrenheit subroutine. ### Conversion Logic | Input unit | Subroutine | Formula | Output unit | | --- | --- | --- | --- | | F | 200 | (T − 32) × 5 / 9 | C | | C | 300 | C × 9/5 + 32 | F | | K | 300 | (T − 273) × 9/5 + 32 | F | Note that Kelvin converts to Fahrenheit only; there is no direct K→C path presented to the user as a final result, though the intermediate Celsius value is computed in `C` at line 310. ### Input Validation Line 70 implements a validation loop by re-routing to line 60 (`GOTO 60`) if the entered string is not one of the three accepted unit codes. This is a common BASIC idiom that keeps the user in a tight loop until valid input is received, without any explicit error message being printed. ### Key BASIC Idioms - **Subroutine dispatch via consecutive IF statements**: Lines 90 and 100 each test `Q$` independently and call the appropriate `GOSUB`. Because the two conditions are mutually exclusive (validated at line 70), only one subroutine fires per pass. - **Pause-via-INPUT**: Line 130 uses `INPUT A$` as a “press ENTER to continue” gate, a standard idiom where the variable `A$` is never actually used. - **Shared subroutine for two input units**: The Kelvin branch shares the Celsius→Fahrenheit subroutine (line 300) by adjusting the intermediate variable `C` at line 310, avoiding code duplication. ### Notable Anomalies - The Kelvin conversion uses 273 as the offset rather than the more precise 273.15. This introduces a small but non-trivial rounding error for scientific use. - The program only converts in one direction per unit pair: F→C, C→F, and K→F. There is no C→K, K→C, or F→K path. - Lines 350 and 360 (`SAVE "1017%2"` and `RUN`) appear to be a save-and-restart tail appended to the listing and are not part of the interactive program flow. ## Source Code ``` 10 REM "TEMP CONV" 20 PRINT "ENTER TEMPERATURE" 30 INPUT T 40 PRINT T 50 PRINT "C, F, OR K"; 60 INPUT Q$ 70 IF Q$<>"F" AND Q$<>"C" AND Q$<>"K" THEN GOTO 60 80 PRINT Q$ 90 IF Q$="F" THEN GOSUB 200 100 IF Q$="C" OR Q$="K" THEN GOSUB 300 110 CLS 120 PRINT T;" ";Q$;"=";N;" ";R$ 130 INPUT A$ 140 CLS 150 GOTO 10 200 LET N=(T-32)*5/9 210 LET R$="C" 220 RETURN 300 LET C=T 310 IF Q$="K" THEN LET C=T-273 320 LET N=C*9/5+32 330 LET R$="F" 340 RETURN 350 SAVE "1017%2" 360 RUN ```