Contents / Part 3 / Previous chapter / Next chapter / Index
Two special arithmetical functions are provided which produce integer (i.e., whole number) results. These integer functions are DIV and MOD. (DIVision and MODulus)
The result of a normal division has two parts - the whole number part and the remainder. Normally the remainder is quoted as a decimal fraction. Thus
11 ÷ 4 = 2.75 or 2¾
However the functions DIV and MOD enable the whole number part and the remainder to be calculated separately.
Thus
11 DIV 4 = 2
(i.e., 4 goes into 11 two times) and
11 MOD 4 = 3
(i.e., the remainder is 3)
A simple division test shows how they can be used.
5 CLS
10 PRINT "Division test!"
20 PRINT "Answer with a whole number, and a" ' "remainder"
30 REPEAT
40 X=RND(100)
50 Y=RND(10)
60 PRINT ' "What is ";X;" divided by ";Y
70 INPUT A
80 INPUT "Remainder? "B
90 IF A=Y DIV Y AND B=X MOD Y THEN PRINT "That's correct" ELSE PRINT "That's wrong"
100 PRINT ' "Press any key to continue"
110 T=GET
120 UNTIL FALSE
DIV and MOD are used whenever you are trying to convert units - for example seconds into minutes. Thus 500 seconds is 500 DIV 60 minutes and 500 MOD 60 seconds that is 8 minutes 20 seconds
For example this program prints a 24 hour clock
5 PRINT "Please input the time"
10 INPUT "Hours ",H
20 INPUT "Minutes ",M
30 TIME=H* 360000 + M* 6000
40 CLS
50 REPEAT
60 SEC=(TIME DIV 100) MOD 60
70 MIN=(TIME DIV 6000) MOD 60
80 HR=(TIME DIV 360000) MOD 24
90 PRINT TAB(7,12) HR;";";MIN;":";SEC
100 UNTIL FALSE
The clock is improved if you type VDU 23; 8202; 0; 0; 0; which switches off the flashing cursor (sec page 77).
The next program would keep time to the end of the century if you left the computer switched on that long!
10 lastminute=0
20 MODE7
30 PROCOFF
40 PROCgetdatetime
50 CLS
60 REPEAT
70 PROCshowtime
80 UNTIL FALSE
90 END
100
110 DEF PROCgetdatetime
120 CLS
130 PRINT"Please supply the day, month and year"
140 PRINT "as numbers e.g. 24 12 1982"
150 PRINT
160
170 REPEAT
180 PRINT TAB(5,10);"Day ";
190 INPUT TAB(12,10) "" day
200 UNTIL day>0 AND day<32
210
220 REPEAT
230 PRINT TAB(5,12);"Month ","
240 INPUT TAB(12,12) "" month
250 UNTIL month>0 AND month<13
260
270 REPEAT
280 PRINT TAB(5,14);"Year";
290 INPUT TAB(12,14) "" year
300 UNTIL year>1799 AND year<2500 OR year>0 AND year<99
310 IF year<99 THEN year=year+1900
320
330 CLS
340 PRINT "and now the time please"
350 PRINT "using a 24 hour clock"
360
370 REPEAT
380 PRINT TAB(5,10);"Hours ";
390 INPUT hour
400 UNTIL hour>-1 AND hour<24
410
4Z0 REPEAT
430 PRINT TAB(5,12);"Minutes ";
440 INPUT minute
450 UNTIL minute>-1 AND minute<60
460
470 TIME=100*60*(minute+60*hour)
480 ENDPROC
490
500
510 DEF PROCshowtime
520 IF TIME>8640000 THEN TIME=TIME-8640000
530 hour=TIME DIV 360000 MOD 24
540 minute=TIME DIV (100*60) MOD 60
550 second=TIME DIV 100 MOD 60
560 IF (hour=0 AND minute=0 AND lastminute=59) THEN PROCincdate
570 lastminute=minute
580 PRINT TAB(0,0);"Date = ";day;" ";
590 RESTORE 600
600 DATA Jan,Feb,Mar,Apr,May,June, July,Aug,Sept,Oct,Nov,Dec
610 FOR X=1 TO month
620 READ month$
630 NEXT X
640 PRINT month$;" ";year;" ";
650 PRINT "GMT = ";
660 IF hour<10 THEN PRINT " ";
670 PRINT;hour;" : ";
680 IF minute<10 THEN PRINT" ";
690 PRINT ;minute;" : ";
700 IF second<10 THEN PRINT " ";
710 PRINT;second;" "
720 ENDPROC
730
740 DEF PROCOFF
750 VDU 23;8202;0;0;0;
760 ENDPROC
770
780
790 DEF PROCincdate
800 day=day+1
810 IF (month=2) AND (day>29) THEN day=1 :month=3
820 IF (month=2) AND (day=29) THEN IF NOT FNLEAP(year) THEN day=1:month=3
830 IF ((month=4 OR month=6 OR
month=9 OR month=11) AND (day=31)) THEN day=1: month=month+1
840 IF day>31 THEN day=1:month=month+1
850 IF month>12 THEN month=1:year=year+1
860 ENDPROC
870
880
890 DEF FNLEAP(Y)
900 REM RETURNS TRUE IF Y IS LEAP YEAR
910 IF Y MOD 4=0 AND (Y MOD 100<>0 OR Y MOD 400=0) THEN =TRUE ELSE=FALSE