Qbasic Tutorial for beginners and children (Under Construction)


Writing a Math Program

This small math program will provide for 4 options:

  • Addition
  • Subtraction
  • Multiplication
  • Division

The concepts that you should know to understand this program are:

DECLARE SUB addition ()
DECLARE SUB subtraction ()
DECLARE SUB multiplication ()
DECLARE SUB division ()
DECLARE SUB menu ()

CLS
RANDOMIZE TIMER

'Displaying the menu
CALL menu

SUB addition
  DIM number1 AS INTEGER
  DIM number2 AS INTEGER
  DIM answer AS INTEGER
  number1 = INT(RND * 100) + 1
  number2 = INT(RND * 100) + 1
  answer = number1 + number2
  PRINT "Addition"
  PRINT number1; " + "; number2; " = "

  INPUT "Enter your answer"; choice
  IF choice = answer THEN
    PRINT "Good answer"
  ELSE
    PRINT "Wrong answer"
    PRINT "The correct answer is"; answer
  END IF
  DO
  LOOP UNTIL INKEY$ <> ""
END SUB

SUB division
  DIM number1 AS INTEGER
  DIM number2 AS INTEGER
  DIM answer AS INTEGER
  number2 = INT(RND * 50) + 1
  number1 = number2 * (INT(RND * 10) + 1)

  answer = number1 / number2
  PRINT "Division"
  PRINT number1; " "; CHR$(246); " "; number2; " = "

  INPUT "Enter your answer"; choice
  IF choice = answer THEN
    PRINT "Good answer"
  ELSE
    PRINT "Wrong answer"
    PRINT "The correct answer is"; answer
  END IF
  DO
  LOOP UNTIL INKEY$ <> ""

END SUB

SUB menu
  DO
    DIM choice AS INTEGER
    CLS
    PRINT "Simle maths program"
    PRINT
    PRINT "1. Addition"
    PRINT "2. Subtraction"
    PRINT "3. Multiplication"
    PRINT "4. Division"
    PRINT "5. Exit"
    INPUT "Enter your choice"; choice
    SELECT CASE choice
      CASE 1
        CALL addition
      CASE 2
        CALL subtraction
      CASE 3
        CALL multiplication
      CASE 4
        CALL division
    END SELECT
  LOOP UNTIL choice = 5
END SUB

SUB multiplication
  DIM number1 AS INTEGER
  DIM number2 AS INTEGER
  DIM answer AS INTEGER
  number1 = INT(RND * 100) + 1
  number2 = INT(RND * 100) + 1  

  answer = number1 * number2
  PRINT "Multiplication"
  PRINT number1; " * "; number2; " = "  
  INPUT "Enter your answer"; choice
  IF choice = answer THEN
    PRINT "Good answer"
  ELSE
    PRINT "Wrong answer"
    PRINT "The correct answer is"; answer
  END IF
  DO
  LOOP UNTIL INKEY$ <> ""

END SUB

SUB subtraction
  DIM number1 AS INTEGER
  DIM number2 AS INTEGER
  DIM answer AS INTEGER
  number1 = INT(RND * 100) + 1
  number2 = INT(RND * 100) + 1
  IF number1 < number2 THEN
    SWAP number1, number2
  END IF

  answer = number1 - number2
  PRINT "Subtraction"
  PRINT number1; " - "; number2; " = "

  INPUT "Enter your answer"; choice
  IF choice = answer THEN
    PRINT "Good answer"
  ELSE
    PRINT "Wrong answer"
    PRINT "The correct answer is"; answer
  END IF
  DO
  LOOP UNTIL INKEY$ <> ""

END SUB

 


pages viewed since January  19 2002