Qbasic, Quickbasic/computer

Advertisement


Question
1)write a program to find the hcf and lcm of two numbers.
2)write a program to generate the series:
i)10,12,15,19....10th term
ii)50,46,31,45....10th term
3)write a program to enter 20 numbers and display the greatest one.(using array)
Its just a problem which i dont know n expecting answer from you.Its not a homework.

Answer
Hi Aanjana,

I still think this is homework (I know this almost as a fact, because no one ever has a reason to learn QBasic these days, plus the structure of the questions is very suspicious and similar to homework), even though you say it is not, but I will help you just a bit more.

Highest common factor is an easier solved problem than you might think, using MOD (division remainer) and a FOR/NEXT loop.

Let's say we wanted to find the highest factor of 10, this is what I would do:

REM num% is going to be the number we are finding a factor of
DIM num%
num = 10

REM Now we're going to loop through all the numbers, from the number we are factoring minus 1 (in this case, 10-1=9) to 1,
REM Our FOR/NEXT loop will have to have a step of -1
REM This is basically how we do math in our heads, we just don't think of that as FOR/NEXT loops.
FOR i% = num%-1 TO 1 STEP -1
  REM The MOD operator really just does division, except instead of returning the division result, it returns the remainer, which will only give us whole numbers.
  REM If the remainer is 0, then we know that the number divides evenly into 10, and therefore it is a factor-able number.
  remainer% = num% MOD i%
  IF remainer% = 0 THEN
    REM If this were calculating a common factor, I would want a nested IF statement here to calculate the second remainer
    PRINT "Highest factor of"; num%; "is"; i%
    REM We have our highest factor of the number, so we can jump out of this loop now
    EXIT FOR
  END IF
NEXT i%


You could also use this loop in reverse to calculate the LCM by switching the order of numbers in the FOR statement, and removing the STEP -1

The second question is a simpler version of learning the FOR/NEXT loop using STEP.  STEP really just says you want to increment or deincrement the number in a certain way.  For instance:

FOR i% = 1 TO 10
  PRINT i%
NEXT i%


Is the exact same as:

FOR i% = 1 TO 10 STEP 1
  PRINT i%
NEXT i%


Both will output:

1
2
3
4
5
6
7
8
9
10


QBasic will always assume STEP 1 unless you tell it differently.  Now, what happens when you only want to print every other number?

FOR i% = 1 TO 10 STEP 2
 PRINT i%
NEXT i%

This will output:

1
3
5
7
9


And what if you wanted these to only be even numbers divisible by 2?

FOR i% = 0 TO 10 STEP 2
  PRINT i%
NEXT i%


The output would be:

0
2
4
6
8
10


Or in reverse:

FOR i% = 10 to 0 STEP -2
  PRINT i%
NEXT i%


Output:

10
8
6
4
2
0


That should help you with 2.i and 2.ii.


Arrays and FOR/NEXT loops work really well together, since both deal with beginning, ends, and sizes.

DIM my_array%(1 TO 10)

FOR i% = 1 TO 10
  my_array%(i%) = i% * 2
  PRINT "my_array%("; i%; ") = "; my_array%(i%)
NEXT i%


Output:

my_array%( 1 ) = 2
my_array%( 2 ) = 4
my_array%( 3 ) = 6
my_array%( 4 ) = 8
my_array%( 5 ) = 10
my_array%( 6 ) = 12
my_array%( 7 ) =14
my_array%( 8 ) = 16
my_array%( 9 ) = 18
my_array%( 10 ) = 20


And getting input is pretty easy, too!

INPUT "Give me a number: ", number%
PRINT "Number is"; number%


It works the same with an array:

DIM cool_numbers%(1 TO 3)

FOR i% = 1 TO 3
  INPUT "Give me a cool number: ", cool_numbers%(i%)
NEXT i%


Finding the biggest number in an array isn't too bad, either.

DIM an_array_with_numbers%(1 TO 500)

REM Since we know we will be comparing all the numbers, we will want to save the one that is the highest.
REM To start, we will want to store the first number in the array, since we don't have a way of knowing the lowest number (so we can't just set it to 0)
DIM largest_number% = an_array_with_numbers%(1)

REM Notice that we are skipping the 1st index, because we have already stored it.  There is no harm in doing a comparison of it, though, since a number cannot be greater or less than itself.
FOR i% = 2 TO 500
  num% = an_array_with_numbers(i%)
  IF num% > largest_number%
    largest_number% = num%
  END IF
NEXT i%

PRINT "Largest number: "; largest_number


That should help you out, while also not giving you any of the answers directly.

Good luck!
-Alex

Qbasic, Quickbasic

All Answers


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

©2016 About.com. All rights reserved.