Qbasic, Quickbasic/quick basic

Advertisement


Question
i have told to take out the sum of first five even numbers..
how to do...
explain

Answer
Hi, Vasudha,

Well, I don't know what you mean by "take out the sum" but I can tell you have to check to see if a number is even or not:

DECLARE FUNCTION IsEven%( num AS INTEGER )

DIM evenNumbers(1 to 5) AS INTEGER ' A list of even numbers we find
DIM j AS INTEGER ' Index of the above array
DIM n AS INTEGER ' Number we are testing to see if it's even

' Set both numbers to 1
n = 1 ' 0 isn't an even number, so we won't start there
j = 1 ' The first index of our array is 1
DO
 IF IsEven%( n ) = 1 THEN  ' If the current number is even...
   evenNumbers( j ) = n    ' Add the number to the array
   j = j + 1               ' Increment the index for the array (for the next number we find)
 END IF
 n = n + 1 ' Go to the next number
LOOP UNTIL j > 5 ' If we have 5 even numbers, then finish the loop

j = 0 ' Now, this variable is going to be the sum of the even numbers:
FOR n = 1 to 5 ' Loop through the whole array
 j = j + evenNumbers( n ) ' Add all the even numbers up
NEXT n

PRINT "Sum of the first 5 even numbers: "; j

FUNCTION IsEven%( num AS INTEGER )
 IsEven% = 0 ' Assume the number isn't even
 DIM test AS INTEGER ' This is the maximum number we'll test for (see below)
 DIM i AS INTEGER ' The numbers we have to test
 test = ( num \ 2 ) - 1 ' The maximum number is at most half of the number we're testing (because once we hit the half way point, the math gets reversed, but will still be the same (like 2+5 is the same as 5+2))

 FOR i = 1 TO test ' Test each number from 1 to test
   IF num MOD i = 0 THEN ' MOD is the remainder from division (if it's 0, then it divides in evenly, and there is no remainder
     IsEven% = 1 ' If it does test well, then the function says it's even
     EXIT FUNCTION ' Exit the function (because we only need 1 number to test it right)
   END IF
 NEXT i
END FUNCTION

I hope that helps you out, and if not, just ask another question,
I hope the commenting helps, too,
-Alex

Qbasic, Quickbasic

All Answers


Answers by Expert:


Ask Experts

Volunteer


Alex Barry

Expertise

I have been a qbasic programmer since 2000, creating games, minor libraries and various small programs. I have experience using interrupts, graphics, file input/output, the mouse cursor, and using libraries. I have also learned FreeBASIC, QB64, c/c++, python, lua, php and html.
I do not claim to be an absolute authority in any language, but I don't mind looking things up and learning with you.

Experience

I have been programming in *Basic dialects since 2000, as mentioned in my expertise. After a year of QBasic, I learned C and C++, and dabbled a little in ASM (I don't program in ASM - I literally just played around to see how things work). When QB64 and FreeBASIC were released, I played with those languages. At the time, FreeBASIC offered more functionality and I sided with that language for a while. During that time, while I was learning new languages, that I would see what scripting languages are available, where I took up python and lua. I started to notice a staleness to QB64's development (which I kept tabs on from time to time), and am now trying to be active in it's community and maybe in it's development in the future. Currently, I am only active on the QB64.net forums, but I appear on occasion on FreeBASIC.net's forums as well.

Education/Credentials
Highschool - 2007

©2012 About.com, a part of The New York Times Company. All rights reserved.