QBasic 1.1

Version of implementation QBasic of programming language Basic

Last version of QBasic.

Examples:

Factorial - Basic (32):

This example uses recursive factorial definition.

The default data type for calculations is floating point, so program output looks like this:
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
11! = 3.99168Е+07
12! = 4.790016Е+08
13! = 6.227021Е+09
14! = 8.717829Е+10
15! = 1.307674Е+12
16! = 2.092279Е+13

DECLARE FUNCTION factorial (n)

FOR i = 0 TO 16:
    PRINT STR$(i) + "! =" + STR$(factorial(i))
NEXT i
END

FUNCTION factorial (n)
    IF n = 0 THEN
        factorial = 1
    ELSE
        factorial = n * factorial(n - 1)
    END IF
END FUNCTION

Fibonacci numbers - Basic (33):

Numbers which have already been calculated are stored in array F and are retrieved from it to calculate the next ones. To get program output in required format, the numbers in the array are concatenated to form one string with required delimiters. STR$ function converts a number to a string.

DIM F(16)
F(1) = 1
F(2) = 1
FOR i = 3 TO 16:
    F(i) = F(i - 1) + F(i - 2)
NEXT i
DIM S AS STRING
S = ""
FOR i = 1 TO 16:
    S = S + STR$(F(i)) + ", "
NEXT i
S = S + "..."
PRINT S

Fibonacci numbers - Basic (34):

This example uses recursive definition of Fibonacci numbers. Each call of PRINT function prints the arguments to a separate line and adds a space before and after printed number, so program output looks like this:
1 ,
1 ,
2 ,
3 ,
5 ,
8 ,
13 ,
21 ,
34 ,
55 ,
89 ,
144 ,
233 ,
377 ,
610 ,
987 ,

DECLARE FUNCTION fibonacci (n)

FOR i = 1 TO 16:
    PRINT fibonacci(i); ", "
NEXT i
PRINT "..."

FUNCTION fibonacci (n)
    IF (n <= 2) THEN
        fibonacci = 1
    ELSE
        fibonacci = fibonacci(n - 1) + fibonacci(n - 2)
    END IF
END FUNCTION

Fibonacci numbers - Basic (35):

Fibonacci numbers are calculated using Binet’s formula. The resulting numbers can differ from actual ones slightly due to calculation imprecision; to remove this effect, we use function INT which truncates fractional part of the number.

DECLARE FUNCTION FIBONACCI (n)

DIM S AS STRING
S = ""
FOR i = 1 TO 16:
    S = S + STR$(INT(FIBONACCI(i) + .1)) + ","
NEXT i
S = S + "..."
PRINT S

FUNCTION FIBONACCI (n)
    p1 = ((1 + SQR(5)) * .5) ^ n
    p2 = ((1 - SQR(5)) * .5) ^ n
    FIBONACCI = (p1 - p2) / SQR(5)
END FUNCTION

Factorial - Basic (224):

This example uses iterative factorial definition. Arithmetic overflow happens when trying to calculate 13!, but different implementations handle it in different ways: QBasic reports an exception, while QuickBasic just proceeds printing negative values.

DIM f AS LONG
f = 1
PRINT " 0 ! ="; f
FOR i = 1 TO 16:
    f = f * i:
    PRINT i; "! ="; f
NEXT i
END

Hello, World! - Basic (31):

PRINT "Hello, World!"