Get Answers to all your Questions

header-bg qa

How to Write a Program that is like a calculator in QBasic ??

Answers (1)

best_answer

Below is a code to create a calculator program in Qbasic that asks the user to enter 2 numbers and then asks the user to choose from options to add, subtract, multiply or divide the two numbers. Input validation has to be used in this code to identify if the user has entered the correct data. This program will also ask users to continue or exit in the end after the results. 

  • Start your Qbasic program 
  • Enter the code 

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

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

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

COLOR 4

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
COLOR 7

END SUB

  • Press "F5" to run the program.

 

Posted by

Deependra Verma

View full answer