This program solves quadratic equations of the form AX² + BX + C = 0 using the quadratic formula, handling both real and complex roots. It computes the discriminant S = B²−4AC; if negative, it calculates the real and imaginary parts separately and displays the complex conjugate pair in the form a ± bI. The program uses SLOW mode during input and output for readability, switching to FAST mode during computation. A DIM A$(1) declaration at line 16 allocates a single-character string for the yes/no prompt response. Input validation ensures coefficient A is non-zero, preventing division by zero in the root calculations.
Program Analysis
Program Structure
The program is organized into a straightforward linear flow with two branch points. Lines 10–46 handle setup and coefficient input, lines 47–109 perform computation and output results, and lines 110–160 manage the repeat-or-quit loop. The overall structure is:
- Initialize display and prompt for coefficients A, B, C (lines 10–46)
- Compute discriminant and roots (lines 47–109)
- Ask user to repeat or quit (lines 110–160)
- Save line at 170–180 (never reached during normal execution)
Mathematical Approach
The discriminant is calculated at line 50 as S = B*B - 4*A*C. Line 60 computes R = SQR(ABS S), taking the absolute value before the square root so that the same variable R holds the magnitude regardless of the sign of S. The branch at line 70 then determines whether roots are real (S ≥ 0) or complex (S < 0).
For real roots, line 85 prints both values directly using the quadratic formula: (-B-R)/(2*A) and (R-B)/(2*A). For complex roots, line 102 reassigns S to hold the imaginary coefficient R/(2*A), and lines 103–104 print the conjugate pair in the form a + bI / a - bI.
Key BASIC Idioms
DIM A$(1)at line 16 — allocates a one-character string variable for the Y/N response, required before string input can be accepted.SLOW/FASTtoggling (lines 15, 47, 115) — switches between display-synchronised and full-speed execution modes at appropriate points.IF A=0 THEN GOTO 33at line 36 — simple validation loop that re-prompts until a non-zero coefficient A is entered, preventing a division-by-zero error.IF A$="Y" THEN RUNat line 135 — restarts the entire program from the beginning rather than using a GOTO, which cleanly reinitializes all variables.
Notable Techniques
The reuse of variable S at line 102 is a minor space-saving idiom: after the discriminant has served its purpose in the branch test, the variable is repurposed to store the imaginary part of the complex roots. This avoids introducing an additional variable at the cost of some readability.
Lines 30–31 simulate a superscript “2” for the equation display by printing it on the line above the formula text, achieving a rudimentary typeset appearance within the constraints of the character-based display.
Bugs and Anomalies
- The
PRINT "ENTER COEFFICIENT A";at line 33 is paired withINPUT Aat line 34, thenPRINT " ";Aat line 35. The echo print after INPUT is somewhat redundant since the value is already visible on-screen from the INPUT prompt, but causes no error. - Lines 150–160 form a fallthrough handler for invalid yes/no input, printing a message and looping back to line 120. However, line 150 says “ENTER YES OR NO” while the program only accepts single characters “Y” or “N” — a minor mismatch between the prompt text and actual validation logic.
- Lines 170–180 (SAVE and GOTO 10) are only reachable if execution somehow falls through line 160’s
GOTO 120, which cannot happen under normal conditions. They serve as a save-and-restart block intended to be run manually or jumped to directly.
Variable Summary
| Variable | Purpose |
|---|---|
A | Coefficient of x² term; also reused as yes/no string variable name prefix |
B | Coefficient of x term |
C | Constant term |
S | Discriminant (B²−4AC); later repurposed as imaginary coefficient |
R | Square root of |S| |
A$ | Single-character user response (“Y” or “N”) |
Content
Image Gallery
Source Code
10 CLS
15 SLOW
16 DIM A$(1)
20 PRINT "ROOTS OF QUADRATIC EQUATIONS"
25 PRINT
30 PRINT " 2"
31 PRINT "WHERE AX +BX +C = 0"
32 PRINT
33 PRINT "ENTER COEFFICIENT A";
34 INPUT A
35 PRINT " ";A
36 IF A=0 THEN GOTO 33
38 PRINT "ENTER COEFFICIENT B";
39 INPUT B
40 PRINT " ";B
41 PRINT "ENTER COEFFICIENT C";
45 INPUT C
46 PRINT " ";C
47 FAST
50 LET S=B*B-4*A*C
60 LET R=SQR (ABS S)
70 IF S<0 THEN GOTO 100
80 PRINT "THE ROOTS ARE REAL AND ARE:"
85 PRINT (-B-R)/(2*A);" AND ";(R-B)/(2*A)
90 GOTO 110
100 PRINT "THE ROOTS ARE COMPLEX NUMBERS:"
102 LET S=R/(2*A)
103 PRINT " ";-B/(2*A);" + ";S;" I"
104 PRINT "AND ";-B/(2*A);" - ";S;" I"
110 PRINT
115 SLOW
120 PRINT "DO YOU WANT TO DO ANOTHER ONE?"
130 INPUT A$
135 IF A$="Y" THEN RUN
140 IF A$="N" THEN STOP
150 PRINT "ENTER YES OR NO"
160 GOTO 120
170 SAVE "QUADRATI[C]"
180 GOTO 10
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.