AllExperts > Qbasic, Quickbasic 
Search      
Qbasic, Quickbasic
Volunteer
Answers to thousands of questions
 Home · More Qbasic, Quickbasic Questions · Answer Library  · Encyclopedia ·
More Qbasic, Quickbasic Answers
Question Library

Ask a question about Qbasic, Quickbasic
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About 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, c/c++, python, 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
Hobby programming since 2000

I no longer belong to any community programming groups, but do have knowledge of *basic dialects and C/C++

 
   

You are here:  Experts > Computing/Technology > Basic > Qbasic, Quickbasic > quick basic

Qbasic, Quickbasic - quick basic


Expert: Alex Barry - 8/28/2009

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

Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.