How To Create A Simple Calculator Program In Qbasic

To create a simple calculator program that asks the user for two numbers. After the computer receives two numbers, the program will prompt the user to add, subtract, multiply or divide the two numbers. the program will display the answer and prompt the user to repeat the program or quit.

Input validation is used within this program. Input validation is used to check to see if the user entered the data correctly.
  •  load your Qbasic program and write the following lines of codes below;



1 CLS
COLOR 7
DIM NUM1 AS SINGLE
DIM NUM2 AS SINGLE
PRINT TAB(30); "A SIMPLE CALCULATOR "
DO WHILE REP$ <> "N"
    PRINT
    INPUT "ENTER FIRST NUMBER HERE"; NUM1
    PRINT
    INPUT "ENTER OPERATOR HERE"; OPERATOR$
    PRINT
    INPUT "ENTER SECOND NUMBER HERE"; NUM2
    PRINT

    CALL OPERAN(NUM1, OPERATOR$, NUM2) 'FOR OPERATORS

    INPUT "TO QUIT PRESS (N), TO CONTINUE PRESS ENTER"; REP$
    REP$ = UCASE$(REP$)
    GOTO 1
LOOP

COLOR 4
LOCATE 23: PRINT TAB(30); "@ Jackieriel-Soft 2016, 08114137088 08131327382"

SUB OPERAN (NUM1, OPERATOR$, NUM2)
COLOR 2
PRINT NUM1; OPERATOR$; NUM2; "=";
IF OPERATOR$ = "+" THEN
    PRINT NUM1 + NUM2
ELSE IF OPERATOR$ = "-" THEN
        PRINT NUM1 - NUM2
    ELSE IF OPERATOR$ = "*" THEN
            PRINT NUM1 * NUM2
        ELSE IF OPERATOR$ = "/" THEN
                PRINT NUM1 / NUM2
            ELSE
                COLOR 4
                PRINT " YOU HAVED ENTERED A WRONG OPERATOR SIGN"
                BEEP
            END IF
        END IF
    END IF
END IF
PRINT
CALL FOOTB


END SUB

SUB FOOTB
COLOR 4
LOCATE 23: PRINT TAB(20); "@ JACKIERIEL SOFT 2016, 08114137088, 08131327382"
COLOR 7

END SUB

  • After you are done with the code press "F5" to run the program.
Note: In this program we use subprogram to accomplished it. You can equally use function or a single program to accomplish it.

If you have any question to ask feel free to contact us.

Post a Comment

0 Comments