This program performs bidirectional temperature conversion between Celsius and Fahrenheit. It presents a menu allowing the user to select C-to-F conversion (option 1), F-to-C conversion (option 2), or program termination (option 3). The C-to-F formula is implemented as (9*C+160)/5, an algebraically equivalent rearrangement of the standard (9/5)*C+32 that avoids floating-point division by 5 until the final step. An error counter variable E tracks invalid menu inputs, terminating the run after more than three mistakes, while a case counter K numbers each successful conversion.
Program Analysis
Program Structure
The program is organised into several distinct regions separated by line number ranges:
- Lines 10–160: Title banner display using
TAB(15)to centre a boxed heading. - Lines 160–310: Initialisation and main menu loop, including error handling for invalid input.
- Lines 320–390: Celsius-to-Fahrenheit conversion branch.
- Lines 400–470: Fahrenheit-to-Celsius conversion branch.
- Lines 480–600: Error termination and normal stop.
- Lines 610–620: Save and re-run utilities.
Conversion Formulae
Both conversions avoid dividing by a non-integer early in the calculation, keeping intermediate values as integers where possible:
| Direction | Formula Used | Standard Form |
|---|---|---|
| C → F | (9*C+160)/5 | (9/5)*C + 32 |
| F → C | (5*F-160)/9 | (5/9)*(F − 32) |
The rearrangement multiplies through by the denominator before adding the offset (32 × 5 = 160), deferring the division to a single operation. This is a clean algebraic optimisation that also happens to reduce floating-point rounding on integer inputs.
Error Handling
Variable E counts invalid menu entries. After each bad input at line 270, E is incremented and checked against 3. The check at line 280 is duplicated at line 300, which is redundant — control can never reach line 300 if E>3 because line 280 would have already branched to 480. This double-check is a minor anomaly but causes no functional problem.
Case Counter
Variable K is incremented at the start of each conversion branch (lines 320 and 400) and printed as a running case number. It is initialised to 0 at line 170 alongside E, ensuring a clean slate each time the program runs from the top. The menu loop (line 390 and 470) returns to line 180 rather than line 230, so the menu prompt is reprinted in full for each new conversion, which is intentional for clarity.
Key BASIC Idioms
- Numeric
INPUTfor the menu choice (Q) rather than string input — any non-numeric entry would cause an error rather than being caught by theEcounter. GOTO 180after each conversion loops back to re-print the menu, effectively making the program re-entrant without a structured loop construct.- Lines 610–620 (
SAVEfollowed byRUN) are a common utility tail found in distributed listings, allowing the program to save itself and immediately restart.
Bugs and Anomalies
- Non-numeric input for
Q,C, orFwill cause a BASIC error rather than a graceful message, sinceINPUTis used without string validation. - The duplicate check at lines 280 and 300 is logically redundant (see Error Handling above).
- The apostrophe in the termination message at line 480 reads
I/M SORRY— this is a typo forI'M, as the apostrophe character is not directly available in the standard character set and has been substituted with a forward slash. - There is a large gap between line 270 (
LET E=E+1) and line 480 (termination message) — lines 600 follows 490 directly, skipping line numbers 500–599; line 500 is referenced by line 260 for normal termination but does not exist, which would cause a “line does not exist” error if the user types 3.
Content
Source Code
10 REM TEMP CONVERSION 2
100 PRINT TAB (15),"**************"
110 PRINT TAB (15),"* *"
120 PRINT TAB (15),"*TEMPERATURE *"
130 PRINT TAB (15),"*CONVERSION *"
140 PRINT TAB (15),"*PROBLEM 2 *"
150 PRINT TAB (15),"**************"
160 LET E=0
170 LET K=0
180 PRINT
190 PRINT "FOR CONVERSION FROM C TO F, TYPE 1"
200 PRINT "FOR CONVERSION FROM F TO C, TYPE 2"
210 PRINT "TO TERMINATE THE PROGRAM, TYPE 3"
220 PRINT
230 INPUT Q
240 IF Q=1 THEN GOTO 320
250 IF Q=2 THEN GOTO 400
260 IF Q=3 THEN GOTO 500
270 LET E=E+1
280 IF E>3 THEN GOTO 480
290 PRINT "EXCUSE ME, ONLY 1,2,OR 3 PLEASE"
300 IF E>3 THEN GOTO 480
310 GOTO 230
320 LET K=K+1
330 PRINT "PLEASE TYPE IN YOUR CELSIUS TEMPERATURE"
340 INPUT C
350 LET F=(9*C+160)/5
360 PRINT
370 PRINT "CASE NUMBER";K
380 PRINT "THE EQUIVALENT FAHRENHEIT TEMPERATURE IS ";F
390 GOTO 180
400 LET K=K+1
410 PRINT "PLEASE TYPE IN YOUR FAHRENHEIT TEMPERATURE"
420 INPUT F
430 LET C=(5*F-160)/9
440 PRINT
450 PRINT "CASE NUMBER";K
460 PRINT "THE EQUIVALENT CELSIUS TEMPERATURE IS ";C
470 GOTO 180
480 PRINT "I/M SORRY, BUT YOU HAVE MADE TOO MANY MISTAKES"
490 PRINT "THEREFORE,YOUR RUN IS TERMINATED"
600 STOP
610 SAVE "1010%8"
620 RUN
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.
