Even and Odd numbers

Just starting out? Need help? Post your questions and find answers here.
User avatar
marcoagpinto
Addict
Addict
Posts: 945
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Even and Odd numbers

Post by marcoagpinto »

Hello!

How do I check if an integer is an even or odd number?

There are no functions to check.

What I did in the 1990s in Pascal was to use the MOD command.

PB too has a MOD command but the help doesn't explain the use of %.

Can someone help?

Thank you!
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 543
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: Even and Odd numbers

Post by bbanelli »

"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
User avatar
marcoagpinto
Addict
Addict
Posts: 945
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Re: Even and Odd numbers

Post by marcoagpinto »

Ahhhh... a lot easier than I thought, thank you!
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Even and Odd numbers

Post by wilbert »

marcoagpinto wrote:How do I check if an integer is an even or odd number?

Code: Select all

For i=1 To 10
  n = Random(100)
  If n & 1
    Debug Str(n)+" is odd"
  Else
    Debug Str(n)+" is even"
  EndIf
Next
or

Code: Select all

For i=1 To 10
  n = Random(100)
  If n % 2
    Debug Str(n)+" is odd"
  Else
    Debug Str(n)+" is even"
  EndIf
Next
but & is much faster compared to %.
Windows (x64)
Raspberry Pi OS (Arm64)
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Even and Odd numbers

Post by Little John »

marcoagpinto wrote:PB too has a MOD command but the help doesn't explain the use of %.
How about opening the help file, clicking at the "index" tab, then double-clicking at the 4th entry in the list?
jack
Addict
Addict
Posts: 1336
Joined: Fri Apr 25, 2003 11:10 pm

Re: Even and Odd numbers

Post by jack »

an integer is odd if bit 0 is set as Wilbert's solution demonstrates.
Post Reply