Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Share:
Notifications
Clear all

[Solved] Pascal - Determine if a number is Odd or Even ...

1 Posts
1 Users
0 Reactions
9,462 Views
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter  

To determine if a number is Odd or Even you could use Odd() function found in several Pascal dialect (for example: in Lazarus, Turbo Pascal and Delphi you'll fund the Odd() function in the System unit).
An equally easy trick is by using the mod operator.

The mod operator (you can find it in most other languages as well: %, mod, and rem or remainder are pretty common) returns the remainder of a division. For example 7 mod 2 would result in 1, since 7/2 = 3 and a remainder of 1 ... (3*2=6)

if MyNumber mod 2 = 0 then 
  ... // This is an EVEN number
else
  ... // This is an ODD number


   
ReplyQuote
Share: