QBASIC Program Examples and Explanations

Z. Perry
The programming language QBASIC is useful for creating fairly complex computer programs to run in Windows or DOS, and is relatively easy to learn. Depending upon the operating system being used, you can either copy and paste these program code examples directly to QBASIC, or save them as .BAS files in a text editor and run them from QBASIC. The following program examples were tested in version 4.5, but should work in other QBASIC versions as well...

IN/CM Converter:

CLS
INPUT "IN:", A
A = A * 2.54
PRINT "CM:"; A

This simple program clears the screen (CLS), then waits for the user to type in (INPUT) the number of inches, which is stored in variable "A". The program multiplies the number of inches by 2.54, to produce the number of centimeters, which is then displayed (PRINT) on the screen.

Word Reverser:

COLOR 15, 1
CLS
INPUT "WORD:", A$
L = LEN(A$)
FOR R = L TO 1 STEP -1
B$ = B$ + MID$(A$, R, 1)
NEXT R
PRINT B$

This somewhat more sophisticated program automatically reverses the letters of a word. First it sets (COLOR) the colors to white (15) text over blue (1), then clears the display. Next, it waits for the user to enter one or more words, which are stored in the variable "A$" (a dollar sign is added to the variable so it can store both numbers and letters). The length (LEN) of "A$" is determined and stored in the variable "L". A FOR/NEXT loop is used by the program to cycle the variable "R" from the number held in "L" down (STEP -1) to the number 1. During the loop, each letter in the variable "A$" is added to "B$" using MID$, which allows letters in certain positions of an alphanumeric variable to be selected. Finally, the program displays (PRINT) the reversed word in "B$" on-screen. Two examples of responses by this program are "tseW" and "NEERG" after entering "West" and "GREEN".

Large Asterisk:

SCREEN 1
LINE (50, 50)-(100, 100), 1
LINE (75, 50)-(75, 100), 1
LINE (50, 75)-(100, 75), 1
LINE (50, 100)-(100, 50), 1

This short program draws a large blue asterisk on the screen. First, it enters screen mode 1, which has fewer colors than the default mode (zero) but allows graphics. This mode does not work properly in Windows XP; you will have to run QBASIC after restarting the computer with a DOS boot disk, or use a different computer, if you have XP. The program uses four LINE commands to draw four overlapping lines which form the asterisk. The numbers in parentheses are the starting coordinates (first two numbers) and ending coordinates (3rd and 4th) on the screen; the last number refers to the drawing color (in QBASIC 1=blue).

These examples only provide a small sample of the capabilities QBASIC has to offer. More tutorials and program examples for QBASIC are available at petesqbsite.com and qbasicstation.com.

Published by Z. Perry

Freelance writer, website operator, and programmer  View profile

4 Comments

Post a Comment
  • nishtha7/29/2010

    mad persons

  • Megha11/15/2009

    THIS IS VERY GOOD. IT IS EASY TO FIND OUT PROGRAMS

  • Matei Florin10/11/2009

    well suppose we have a sorted array and we do a few "rolls" with its terms: Euclid algo might help to put them in their "original" posns, linear time... matei_virgiliu_florin@yahoo.com

  • Matei Florin10/11/2009

    mateif64@yahoo.com
    cos(3x-x)=
    =a*cos(x)+b*sin(x)=
    =cos(x)*cos(x)-sin(x)*sin(x)=
    =cos(2x)

Displaying Comments

To comment, please sign in to your Yahoo! account, or sign up for a new account.