Armstrong Numbers

Highlight the following code and save it as a text file called ARMSTRNG.BAS
Put it in the same folder as GW-BASIC.EXE. Load it and run it.

10 ' PROGRAM Armstrong Numbers
20 CLEAR :KEY OFF :CLS
30 PRINT "ARMSTRONG NUMBERS" :PRINT
40 PRINT "Program computes all Armstrong numbers in the range of 0 - 999."
50 PRINT "An Armstrong number is a number such that the sum of its digits"
60 PRINT "raised to the third power is equal to the number itself." :PRINT
70 '
80 '   A, B, C                             the three digits
90 '   ABC, A3B3C3                         the number and its cubic sum
100 '   COUNT                              a counter
110 '
120 A=0: B=0: C=0: COUNT=0                 'initialise
130 FOR A = 0 TO 9                         'for the left most digit
140   FOR B = 0 TO 9                       'for the middle digit
150     FOR C = 0 TO 9                     'for the right most digit
160       ABC = A*100 + B*10 + C           'the number
170       A3B3C3 = A^3 + B^3 + C^3         'the sum of cubes
180       IF ABC = A3B3C3 THEN GOSUB 230   'if they are equal
190     NEXT C
200   NEXT B
210 NEXT A
220 END
230 ' Display results subroutine
240 COUNT = COUNT + 1                      'count the Armstrongs
250 PRINT "Armstrong number"; COUNT;": "; USING "####"; ABC
260 RETURN

Previous | Home | Next