You are here:

# Qbasic, Quickbasic/qbasic coding for odd/even

Question
QUESTION: Hello,
Can you PLEASE help me with coding this short program, as I don't remember how to do it any more; what I want to accomplish is to categorize  six (random) numbers I input, into how many odd and how many even number there are. Example: INPUT "Enter 6 random numbers (just two Digits)"?

4 23 37 38 40 48
and, after I "RUN" program, I would get:
Even=4/Odd=2
Here's another example: enter 6 random numbers etc....

23 36 38 41 45 48
I would get the following results:

Even=3/Odd=3  (the printing should be on just ONE line)

I have tried (see below) some coding but, the COUNTERS (for even/odd) are not right and I believe they should be inside a LOOP..I gave up on it. Also, I'd like to separate, as I enter/input the number, by a comma, but I got garbage , when I separated them by a ","

5 CLS
7 PRINT "enter 6 random (two digits only) numbers separated by a comma";
8 INPUT c
10 FOR V = 1 TO 6
11    IF INT(c / 2) = c / 2 THEN 12
12      LET A = 0
30      LET A = A + 1
31    GOTO 46
32      LET B = 0
45      LET B = B + 1
46 NEXT V
47 PRINT
48 PRINT
49 PRINT TAB(5); "EVEN="; B; TAB(20); "/ODD="; A
50 END

Thanks,
Don.

I'm not sure if you're programming for GW Basic or QBasic, but some of your structure is quite puzzling.

The first thing that you'll want to do is change your IF/GOTO statements to look like this:

```11    IF INT(c / 2) = c / 2 THEN
12      LET A = 0
30      LET A = A + 1
31    ELSE
32      LET B = 0
45      LET B = B + 1
46    END IF```

Next, I'm curious if you actually tried running that.  One thing that you'll notice when you do is that you will always have A=1, and if it printed B, it may give 0, a random number, or an error because you're skipping over where you're setting B.

To fix this, you'll want to first initialize A and B to 0 before the FOR/NEXT loop.  Then, inside the IF/ELSE/END IF blocks, you'll want to remove where you set A and B to 0.

Lastly, your even number checker will work, but you may find it easier to use the modulus operator (which finds the remainder in a division);

`IF (c MOD 2) = 0 THEN`

The MOD operator works by dividing the left side (c) with the right side (2), and returns the remainder of the division.  If there is no remainder then the division wasn't a float/single, and thus it divides evenly in.

Do those fixes, make sure you understand the code, and give it a run.  If it gives you an error, QBasic gives pretty sane error messages that you can try debugging - if you still can't figure it out, that's okay, fire off another question :)

Take care,
-Alex

---------- FOLLOW-UP ----------

QUESTION: Hi Alex,

I have tried BOTH of them and, didn't work as I thought; can you please help with at least ONE of them?. I'm using QB v.7

5 CLS
7 PRINT "enter 6 random (two-digit) numbers NOT separated by a comma";
8 INPUT c
9      LET A = 0
10     LET B = 0
14 FOR V = 1 TO 6
15    IF (c MOD 2) = 0 THEN LET A = A + 1
31    LET B = B + 1
46 NEXT V
47 PRINT
48 PRINT
49 PRINT TAB(5); "EVEN="; B; TAB(20); "/ODD="; A
50 END

ALSO:

5 CLS
7 PRINT "enter 6 random (two-digit) numbers NOT separated by a comma";
8 INPUT c
9      LET A = 0
10     LET B = 0
14 FOR V = 1 TO 6
15    IF (c MOD 2) = 0 THEN LET A = A + 1
16    ELSE B=B+1   (I've got an error message for this line...don't know why)
46 END IF INT(c/2) = c/2
47 PRINT
48 PRINT
49 PRINT TAB(5); "EVEN="; B; TAB(20); "/ODD="; A
50 END

Thank you,
Don

Your second one is so close you can almost taste it :)

```5 CLS
7  PRINT "enter 6 random (two-digit) numbers NOT separated by a comma";
8  INPUT c
9  LET A = 0
10 LET B = 0
14 FOR V = 1 TO 6
15    IF (c MOD 2) = 0 THEN
16      LET A = A + 1
17    ELSE
18      LET B=B+1   '' Added a missing "LET" just to be consistent
45    END IF
REM 46    INT(c/2) = c/2 '' Not sure why you have this line, you can safely remove it
47 NEXT V '' You missed closing the FOR/Next loop
48 PRINT
49 PRINT
50 PRINT TAB(5); "EVEN="; A; TAB(20); "/ODD="; B '' You had your odd/evens mixed up
51 END```

If you have any questions about the changes I made, just let me know and I can talk you through it :)

Take care,
-Alex

---------- FOLLOW-UP ----------

QUESTION: Hi Alex,

It STILL doesn't work as it should be - for example, if I enter the following numbers: 2, 3, 4, 5, 6, 7 the result I'm getting is:
Even=0/Odd=6 which is WRONG (it should be EVEN=3/ODD=3)!. I also tried these numbers: 11,2,13,5,7,9 and am still getting the WRONG results; not only that, but  when I enter: 2,22,24,26,28,30 OR 20,30,40,42,46,48 I'm getting entire line 15 HIGHLIGHTED and an error message: OVERFLOW".

5 CLS
7 PRINT "enter 6 random (two-digit) numbers NOT separated by a comma";
8 INPUT c
9      LET A = 0
10     LET B = 0
14 FOR V = 1 TO 6
15    IF (c MOD 2) = 0 THEN
16       LET A = A + 1
17    ELSE
18       LET B = B + 1
45    END IF
46 NEXT V
47 PRINT
48 PRINT
49 PRINT TAB(5); "EVEN="; A; TAB(20); "/ODD="; B
50 END

Why doesn't it work as it should?. Can you PLEASE help me?.
Thank you, Don.

Hi Don, sorry, I missed moving one line, your code should look like this:

```5 CLS
7  PRINT "enter 6 random (two-digit) numbers NOT separated by a comma";
8  LET A = 0
9  LET B = 0
10 FOR V = 1 TO 6
11   PRINT "Input Number ["; V; "/6]";
12   INPUT ": ", c
15    IF (c MOD 2) = 0 THEN
16       LET A = A + 1
17    ELSE
18       LET B = B + 1
45    END IF
46 NEXT V
47 PRINT
48 PRINT
49 PRINT TAB(5); "EVEN="; A; TAB(20); "/ODD="; B
50 END```

Note I moved the INPUT into the FOR/NEXT loop, plus a print statement to let the user know how many numbers they need to input.

Take care, have a good new year!
-Alex
Questioner's Rating
 Rating(1-10) Knowledgeability = 10 Clarity of Response = 10 Politeness = 10 Comment Very SMART individual - very helpful!.

• Ask a Question

Qbasic, Quickbasic

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