Qbasicnews.com
February 23, 2020, 07:23:19 AM
 Welcome, Guest. Please login or register. 1 Hour 1 Day 1 Week 1 Month Forever Login with username, password and session length
 Home Help Search Login Register
 Pages: [1] 2 3
 Author Topic: Odd or even?  (Read 10267 times)
TECHsector
New Member

Posts: 5

 « on: April 01, 2003, 01:33:05 PM »

How do you make a program that asks the user to input a number then output a print statement telling if it's odd or even
 Logged
na_th_an
*/-\*

Posts: 8244

 « Reply #1 on: April 01, 2003, 02:24:27 PM »

Well... To know if a number is odd or even you can do several things, for example dividing it by 2 and checking if the remaining is 0 or 1. There is a function, "MOD", which returns the remaining:

Code:
IF number% MOD 2 = 0 THEN PRINT "Even" ELSE PRINT "Odd"

You can also do a logic AND with 1:

Code:
IF (number% AND 1) = 0 THEN PRINT "Even" ELSE PRINT "Odd"

Or you can also try if integer division equals floating division:

Code:
IF number%\2 = number%/2! THEN PRINT "Even" ELSE PRINT "Odd"
 Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
relsoft
*/-\*

Posts: 3927

 « Reply #2 on: April 02, 2003, 03:33:22 AM »

More ELEGANT!!!!

if N=1 then Print "Odd"
if N=2 then Print "EVEN"
if N=3 then Print "Odd"
if N=4 then Print "EVEN"
if N=5 then Print "Odd"
if N=6 then Print "EVEN"
if N=7 then Print "Odd"
if N=8 then Print "EVEN"
if N=9 then Print "Odd"
if N=10 then Print "EVEN"
if N=11 then Print "Odd"
if N=12 then Print "EVEN"
if N=13 then Print "Odd"
if N=14 then Print "EVEN"
if N=15 then Print "Odd"
if N=16 then Print "EVEN"
if N=17 then Print "Odd"
if N=18 then Print "EVEN"
:
:
:
if N=999999999 then Print "ODD"

Fool Proof!!!!!
 Logged

y smiley is 24 bit.

Genso's Junkyard:
http://rel.betterwebber.com/
Hexadecimal Disaster
Been there, done that

Posts: 1123

 « Reply #3 on: April 02, 2003, 03:41:45 AM »

LOL

That's the shortcut method, Rel?
 Logged

img]http://usuarios.vtr.net/~disaster/sigs/annoyizer.php[/img]
relsoft
*/-\*

Posts: 3927

 « Reply #4 on: April 02, 2003, 04:23:33 AM »

I've been using it for years now. ;*)
 Logged

y smiley is 24 bit.

Genso's Junkyard:
http://rel.betterwebber.com/
Neo
Na_th_an

Posts: 2150

 « Reply #5 on: April 02, 2003, 05:11:16 AM »

And having exe of some megabytes eh?
 Logged
Hard Rock
I hold this place together

Posts: 775

 « Reply #6 on: April 02, 2003, 12:54:50 PM »

Nah qb doesnt make .exe's bigger than 600kb(for obvious memory reasons)

Thats what vb is for :p
 Logged

b]Hard Rock[/b]
[The Stars Dev Company] [Metal Qb flopped] [The Terror]
Stop Double Posts!
Whats better? HTML or Variables?
TECHsector
New Member

Posts: 5

 « Reply #7 on: April 02, 2003, 01:34:07 PM »

Ok... now, how about greatest common factor of 2 numbers?
 Logged
toonski84
__/--\__

Posts: 2567

 « Reply #8 on: April 02, 2003, 03:49:35 PM »

well, here's the poor man's method, untested and not ready to go :

Code:
input x
input y
if y > x then swap x, y

for z = 1 to x
if x \ z = x / z and y \ z = y / z then gcf = z
next z

print gcf
 Logged

i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Agamemnus
x/ \z

Posts: 3491

 « Reply #9 on: April 02, 2003, 06:44:52 PM »

I always used CINT actually.

IF b/2 = cint(b/2) then SYSTEM
 Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
toonski84
__/--\__

Posts: 2567

 « Reply #10 on: April 02, 2003, 07:18:34 PM »

then you're wasting processer power.  an integer divide is always faster than rounding a floating point divide.
 Logged

i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Agamemnus
x/ \z

Posts: 3491

 « Reply #11 on: April 02, 2003, 11:47:25 PM »

and a check to see whether the first bit is on or off is even faster.  :lol:
 Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
toonski84
__/--\__

Posts: 2567

 « Reply #12 on: April 03, 2003, 01:36:06 AM »

...that would be if you were finding out if it was even/odd, not determining the greatest common factor *bitc.hslaps agamemnus*
 Logged

i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Neo
Na_th_an

Posts: 2150

 « Reply #13 on: April 03, 2003, 09:52:57 AM »

heh how about the sea of erathos with prime nrs > 100000? lol
 Logged
na_th_an
*/-\*

Posts: 8244

 « Reply #14 on: April 03, 2003, 10:32:24 AM »

Quote from: "Neo"
heh how about the sea of erathos with prime nrs > 100000? lol

Do you mean the siege of Erathosthenes?
 Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
 Pages: [1] 2 3
Jump to: