Search code examples
qbasic

QBASIC Decimal to Binary conversion


I have converted a decimal number to binary using STR$() in QBASIC. But I need a way to convert decimal number to binary without using string functions. Thanks.

My Code :

CLS
INPUT N
WHILE N <> 0
    E = N MOD 2
    B$ = STR$(E)
    N = FIX(N / 2)
    C$ = B$ + C$
WEND
PRINT "Output "; C$
END

Solution

  • This code sample converts a numeric value to a binary string in Basic.

    PRINT "Enter value";
    INPUT Temp#
    Out3$ = ""
    IF Temp# >= False THEN
        Digits = False
        DO
            IF 2 ^ (Digits + 1) > Temp# THEN
                EXIT DO
            END IF
            Digits = Digits + 1
        LOOP
        FOR Power = Digits TO 0 STEP -1
            IF Temp# - 2 ^ Power >= False THEN
                Temp# = Temp# - 2 ^ Power
                Out3$ = Out3$ + "1"
            ELSE
                Out3$ = Out3$ + "0"
            END IF
        NEXT
    END IF
    PRINT Out3$
    END