The following programs can be copied with selecting the program body except END and be pasted on BASIC to be executed.
Programs that find the solution of the equation ax+b=0 when two numbers a and b are input separated by a comma.
Program 1.
100 INPUT a,b 110 IF a=0 THEN 120 IF b=0 THEN 130 PRINT "Any number" 140 ELSE 150 PRINT "No solutions" 160 END IF 170 ELSE 180 PRINT -b/a 190 END IF 200 END |
Program 2.
100 INPUT a,b 110 IF a<>0 THEN 120 PRINT -b/a 130 ELSE 140 IF b<>0 THEN 150 PRINT "No solutions" 160 ELSE 170 PRINT "Any number" 180 END IF 190 END IF 200 END |
Note. The inequality (≠)is described as "<>" in BASIC.
Program 3.
The logic in program 2 can be represent as follows using ELSEIF.
100 INPUT a,b 110 IF a<>0 THEN 120 PRINT -b/a 130 ELSEIF b<>0 THEN 140 PRINT "No solution" 150 ELSE 160 PRINT "Any number" 170 END IF 180 END |
Programs that answer the year is common or leap when a year is input.
Program 1.
100 INPUT y 110 IF MOD(y,4)=0 THEN 120 IF MOD(y,100)=0 THEN 130 IF MOD(y,400)=0 THEN 140 PRINT "leap" 150 ELSE 160 PRINT "common" 170 END IF 180 ELSE 190 PRINT "leap" 200 END IF 210 ELSE 220 PRINT "common" 230 END IF 240 END |
Note. MOD(a, b) finds the remainder of a divided by b.
Program 2.
100 INPUT y 110 IF MOD(y,4)<>0 THEN 120 PRINT "common" 130 ELSEIF MOD(y,100)<>0 THEN 140 PRINT "leap" 150 ELSEIF MOD(y,400)<>0 THEN 160 PRINT "common" 170 ELSE 180 PRINT "leap" 190 END IF 200 END |
Program 3
10 INPUT y 20 IF MOD(y,4)=0 AND (MOD(y,100)<>0 OR MOD(y,400)=0) THEN 30 PRINT "leap" 40 ELSE 50 PRINT "common" 60 END IF 70 END |
Program 4.
10 INPUT y 20 IF MOD(y,4)<>0 OR (MOD(y,100)=0 AND MOD(y,400)<>0) THEN 30 PRINT "common" 40 ELSE 50 PRINT "leap" 60 END IF 70 END |
As AND exceeds OR in BASIC, the 20-line in Program 4 can be written as follows.
20 IF MOD(y,4)<>0 OR MOD(y,100)=0 AND MOD(y,400)<>0 THEN
Programs that make the multiplication table.
(Examples on nested FOR~NEXT loops and output control)
Program 1
10 FOR a=1 TO 9 20 FOR b=1 TO 9 30 PRINT a*b; 40 NEXT b 50 PRINT 60 NEXT a 70 END |
Result
1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 3 6 9 12 15 18 21 24 27 4 8 12 16 20 24 28 32 36 5 10 15 20 25 30 35 40 45 6 12 18 24 30 36 42 48 54 7 14 21 28 35 42 49 56 63 8 16 24 32 40 48 56 64 72 9 18 27 36 45 54 63 72 81 |
If a PRINT statement has a semicolon at the end, the PRINT statement that shall be executed next shall output items successively.
A positive numbers has a preceding space and a successive space.
A PRINT statement with no items shall only output a line feed.
Program 2
10 SET ZONEWIDTH 4 20 FOR a=1 TO 9 30 FOR b=1 TO 9 40 PRINT a*b, 50 NEXT b 60 PRINT 70 NEXT a 80 END |
Result
1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 3 6 9 12 15 18 21 24 27 4 8 12 16 20 24 28 32 36 5 10 15 20 25 30 35 40 45 6 12 18 24 30 36 42 48 54 7 14 21 28 35 42 49 56 63 8 16 24 32 40 48 56 64 72 9 18 27 36 45 54 63 72 81 |
SET ZONEWIDTH determines the width of the column for an item.
If a PRINT statement has a comma at the end, The PRINT statement that shall be executed next shall output items form the next column with no line feed.
Program 3.
10 FOR a=1 TO 9 20 FOR b=1 TO 9 30 PRINT USING "####": a*b; 40 NEXT b 50 PRINT 60 NEXT a 70 END |
Result
1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 3 6 9 12 15 18 21 24 27 4 8 12 16 20 24 28 32 36 5 10 15 20 25 30 35 40 45 6 12 18 24 30 36 42 48 54 7 14 21 28 35 42 49 56 63 8 16 24 32 40 48 56 64 72 9 18 27 36 45 54 63 72 81 |
PRINT USING outputs items with formats. The format and output-items are separated by a colon.
A PRINT USING statement can have a colon or semicolon at the end.
In this case, the effect is the same as that of a regular PRINT statement.
A Format consists of #'s enclosed by " and ".
One # assigns one digit of the numeric. A numeric is right-aligned in the format.
A program that calculates the factorial of n.
A variable that has been assigned one beforehand shall be multiplied by successive integers from 1 to n.
10 INPUT n 20 LET a=1 30 FOR i=1 TO n 40 LET a=a*i 50 NEXT i 60 PRINT a 70 END |
A program that outputs the sum of the first n terms of the sequence a(n) that is defined in a DEF-statment.
10 DEF a(n)=n^2+3*n+2 20 INPUT n 30 LET s=0 40 FOR k=1 TO n 50 LET s=s+a(k) 60 NEXT k 70 PRINT s 80 END |
A variable s, to which a(1), a(2), ..., a(n) shall be added successively, is prepared for calculation.
"n"s in the DEF-line and "n"s in other lines are different variables, because "n" is indicated as a parameter of a function in the DEF-line.
A program that finds the average of ten numbers written in the DATA statement.
10 DATA 34,29,34,90,12,78,66,85,34,92 20 LET s=0 30 FOR i=1 TO 10 40 READ a 50 LET s=s+a 60 NEXT i 70 PRINT s/10 80 END |
A READ statement read a number successively from the DATA statement.
A program that finds the average of indefinite number of numbers written in the DATA statement.
100 DATA 34,29,34,90,12,78,66,85,13,99,12,67 110 LET n=0 120 LET s=0 130 DO 140 READ IF MISSING THEN EXIT DO : a 150 LET n=n+1 160 LET s=s+a 170 LOOP 180 PRINT "number of items";n 190 PRINT "total";s 200 PRINT "average"; s/n 210 END |
READ IF MISSING THEN EXIT DO terminates the repetition of DO~LOOP when data is exhausted.
Two items written in a PRINT statement separated by a semicolon shall be output consecutively.
Assume that the full marks of a test is 100 points. Make a program that replies the distribution of the marks with class width 10, when the number of pupils is entered and then their marks are entered individually.
100 DIM a(0 TO 10) 110 MAT a=ZER 120 INPUT PROMPT "number of sheets": n 130 FOR k=1 TO n 140 INPUT PROMPT STR$(k)&"-th mark": x 150 IF 0<=x AND x<10 THEN LET a(0)=a(0)+1 160 IF 10<=x AND x<20 THEN LET a(1)=a(1)+1 170 IF 20<=x AND x<30 THEN LET a(2)=a(2)+1 180 IF 30<=x AND x<40 THEN LET a(3)=a(3)+1 190 IF 40<=x AND x<50 THEN LET a(4)=a(4)+1 200 IF 50<=x AND x<60 THEN LET a(5)=a(5)+1 210 IF 60<=x AND x<70 THEN LET a(6)=a(6)+1 220 IF 70<=x AND x<80 THEN LET a(7)=a(7)+1 230 IF 80<=x AND x<90 THEN LET a(8)=a(8)+1 240 IF 90<=x AND x<100 THEN LET a(9)=a(9)+1 250 IF 100=x THEN LET a(10)=a(10)+1 260 NEXT k 270 PRINT " 0s ",a( 0) 280 PRINT " 10s ",a( 1) 290 PRINT " 20s ",a( 2) 300 PRINT " 30s ",a( 3) 310 PRINT " 40s ",a( 4) 320 PRINT " 50s ",a( 5) 330 PRINT " 60s ",a( 6) 340 PRINT " 70s ",a( 7) 350 PRINT " 80s ",a( 8) 360 PRINT " 90s ",a( 9) 370 PRINT "100s ",a(10) 380 END |
MAT a=ZER in line 110 substitutes zeros for all elements of an array a.
Full BASIC has two types of if-statement, IF ~ END IF, which is written spanning multiple lines, and simple IF statement , which is written in one line.
In case that a statement follows THEN, this if statement is a simple IF statement, and then there is no corresponding END IF. But simple IF statement has only one statement followed by THEN.
Program 2
100 DIM a(0 TO 10) 110 MAT a=ZER 120 INPUT PROMPT "number of sheets ": n 130 FOR k=1 TO n 140 INPUT PROMPT STR$(k)&"-th mark ": x 150 FOR i=0 TO 10 160 IF i*10<=x AND x<10*(i+1) THEN LET a(i)=a(i)+1 170 NEXT i 180 NEXT k 190 FOR i=0 TO 10 200 PRINT 10*i;"s ",a(i) 210 NEXT i 220 END |
Program 3
100 DIM a(0 TO 10) 110 MAT a=ZER 120 INPUT PROMPT "number of sheets ": n 130 FOR k=1 TO n 140 INPUT PROMPT STR$(k)&"-th mark ": x 150 LET i=INT(x/10) 160 LET a(i)=a(i)+1 170 NEXT k 180 FOR i=0 TO 10 190 PRINT 10*i;"s ",a(i) 200 NEXT i 210 END |
Make a program that replies the highest mark and the lowest mark when the marks are entered as the above.
100 INPUT PROMPT "number of sheets ": n 110 INPUT PROMPT "first mark ": x 120 LET max=x 130 LET min=x 140 FOR k=2 TO n 150 INPUT PROMPT STR$(k)&"-th mark ": x 160 IF x>max THEN LET max=x 170 IF x<min THEN LET min=x 180 NEXT k 190 PRINT "highest "; max 200 PRINT "lowest "; min 210 END |
When a new mark is entered, if it exceeds the previous highest mark, it shall be the current highest mark.
100 INPUT PROMPT "number of sheets ": n 110 LET max=0 120 LET min=100 130 FOR k=1 TO n 140 INPUT PROMPT STR$(k)&"-th mark ": x 150 IF x>max THEN LET max=x 160 IF x<min THEN LET min=x 170 NEXT k 180 PRINT "highest "; max 190 PRINT "lowest "; min 200 END |
When the range of numerics shall be input is definite, both bounds can be employed as the initial values of highest or lowest values.
Numerical Integration
A program that calculates an approximate value of the definite integral of the function f defined in the DEF statement over the interval [a, b], applying the trapezoidal rule, the mid-point rule and Simpson's rule.
100 DEF f(x)=SIN(x) ! the integrand 110 LET a=0 ! the lower bound 120 LET b=PI ! the upper bound 130 LET n=10000 ! the number of subintervals 140 LET h=(b-a)/n ! the width of a subinterval 150 LET t=(f(a)+f(b))/2 ! the trapeziodal rule begins 160 FOR i=1 TO n-1 170 LET t=t+f(a+i*h) 180 NEXT i 190 LET t=t*h ! approximate value by the trapezoidal rule 200 LET m=0 ! the mid-point rule begins 210 FOR i=0.5 TO n-0.5 220 LET m=m+f(a+i*h) 230 NEXT i 240 LET m=m*h ! the approximate value by the mid-point rule 250 LET s=(t+2*m)/3 ! the approximate value by Simpson's rule 260 PRINT "the trapezoidal rule",t 270 PRINT "the mid-point rule" ,m 280 PRINT "Simpson's rule" ,s 290 END |
Series expansion of sine and cosine
Each term of the expansion of sine or cosine is in the form xn / n!.
Noticing xn / n! = (x/1)・(x/2)・(x/3)・…・(x/n),
this term can be obtained by multiplying a variable that has been given the initial value 1 by x/1,x/2,x/3,…,x/n.
The expansion of the sine function is made from these terms with adding if the remainder of the index divided by 4 is 1, subtracting if it is 3.
100 REM Exapansion of Sine 110 LET x=PI/6 120 LET t=1 130 LET s=0 140 FOR i=1 TO 60 150 LET t=t*x/i 160 SELECT CASE MOD(i,4) 170 CASE 1 180 LET s=s+t 190 PRINT i, s 200 CASE 3 210 LET s=s-t 220 PRINT i, s 230 CASE ELSE 240 END SELECT 250 NEXT i 260 END |
In case of the cosine function, terms are added if the remainder divided by 4 is 0, subtracted if it is 2.
100 REM Expansion of Cosine 110 LET x=PI/6 120 LET t=1 130 LET c=1 140 FOR i=1 TO 60 150 LET t=t*x/i 160 SELECT CASE MOD(i,4) 170 CASE 0 180 LET c=c+t 190 PRINT i, c 200 CASE 2 210 LET c=c-t 220 PRINT i, c 230 CASE ELSE 240 END SELECT 250 NEXT i 260 END |
These two programs can be executed in Decimal BASIC on the decimal mode and the binary mode, but repetition of addition and subtraction decreases the accuracy. It is recommended that they are executed on the 1000-digit decimal mode in order to decrease errors.