Showing posts with label QBASIC. Show all posts
Showing posts with label QBASIC. Show all posts

Sunday, March 31, 2013

QBasic - Checking Palindrome Word

Description:
This tutorial will show how find if a word or string is a palindrome. A palindrome is a word or string that is the same printed forwards and backward.

Examples:
bob
noon
1991
redder

Note: the Oxford English Dictionary states that the longest palindromic word is tattarrattat.

The Guinness Book of Records states that detartrated is the longest word and a soap dish vender saippuakuppinippukauppias is a long palindrome name. (Don’t ask how to pronounce that name).


Note: The program uses LCASE$ instead of UCASE$. LCASE$ converts strings to lowercase.

Code:
DIM Wrd AS STRING
DIM RevWrd AS STRING
DIM x AS INTEGER

CLS

INPUT "Enter Word: ", Wrd

FOR x = LEN(Wrd) TO 1 STEP -1
    RevWrd = RevWrd + MID$(Wrd, x, 1)
NEXT x

PRINT
PRINT "Original Word: "; LCASE$(Wrd)
PRINT "Reverse Word: "; LCASE$(RevWrd)

IF LCASE$(Wrd) = LCASE$(RevWrd) THEN
    PRINT "The Word Is A Palindrome"
ELSE
    PRINT "The Word Is Not A Palindrome"
END IF

Monday, March 25, 2013

QBASIC, Draw a circle

Noun
A round plane figure whose boundary (the circumference) consists of points equidistant from a fixed center.
Verb
Move all the way around (someone or something), esp. more than once: "the two dogs circle each other"; "we circled around the island".
Synonyms
noun.      ring - round - cycle - sphere
verb.      revolve - surround - encircle - wheel - compass
 
NAME
  CIRCLE Statement

SYNOPSIS
  CIRCLE [STEP] (x!,y!),radius![,[color%] [,[start!] [,[end!] [,aspect!]]]]
      o STEP       Specifies that coordinates are relative to the current
                   graphics cursor position.
      o (x!,y!)    The coordinates for the center of the circle or ellipse.
      o radius!    The radius of the circle or ellipse in the units of the
                   current coordinate system, determined by the most recent
                   SCREEN, VIEW, and WINDOW statements.
      o color%     A color attribute that sets the circle's color. The
                   available color attributes depend on your graphics adapter
                   and the screen mode set by the most recent SCREEN statement.
      o start!     The starting angle for the arc, in radians.
      o end!       The ending angle for the arc, in radians.
      o aspect!    The ratio of the length of the y axis to the length of the
                   x axis, used to draw ellipses.
      o To convert from degrees to radians, multiply degrees by (PI / 180).

DESCRIPTION
  Draws a circle or ellipse on the screen.

  Example:
      'This example requires a color graphics adapter.
      SCREEN 2
      CIRCLE (320, 100), 200
      CIRCLE STEP (0,0), 100

Sunday, February 24, 2013

QBASIC Control Statements. QBASIC Looping

QBasic is a programming language, created by Microsoft. It is based upon the original language BASIC (Beginners All-purpose Symbolic Instruction Code).

Control Statements
A program consists of a number of statements which are usually executed in sequence. Control statements allow you to change the computer's control from automatically reading the next line of code to reading a different one. The Control Statements are used for controlling the Execution of the program

There are three types of loops for QBASIC.
  1. While..Wend 
  2. Do Loop 
  3. For Loop
The While Loop’s syntax
WHILE [condition]
[Statement Block]
WEND

Example
CLS
B=1
WHILE B<=9
PRINT B
B=B+1
WEND
END

Tuesday, February 5, 2013

Fibonacci numbers in Basic

Fibonacci numbers can calculated using 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 b = 1 TO 16
    S = S + STR$(INT(FIBONACCI(b) + .1)) + ","
NEXT b
S = S + "..."
PRINT S

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