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!
Even and Odd numbers
- marcoagpinto
- Addict
- Posts: 947
- Joined: Sun Mar 10, 2013 3:01 pm
- Location: Portugal
- Contact:
- marcoagpinto
- Addict
- Posts: 947
- Joined: Sun Mar 10, 2013 3:01 pm
- Location: Portugal
- Contact:
Re: Even and Odd numbers
Ahhhh... a lot easier than I thought, thank you!bbanelli wrote:https://www.purebasic.com/documentation ... ables.html
Re: Even and Odd numbers
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
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
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)
-
- Addict
- Posts: 4527
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: Even and Odd numbers
How about opening the help file, clicking at the "index" tab, then double-clicking at the 4th entry in the list?marcoagpinto wrote:PB too has a MOD command but the help doesn't explain the use of %.
Re: Even and Odd numbers
an integer is odd if bit 0 is set as Wilbert's solution demonstrates.