Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to find odd/even numbers in perl

Status
Not open for further replies.

thendal

Programmer
Aug 23, 2000
284
Hi!

How to find a given number is odd or even,any help is appriciated

Thanks in advance

 
Hello thendal,
Divide by 2 and see if there is anything after the decimal with the 'modulus' operator, %

#!perl
$value = 4;
$odd_num = $value % 2;
if ($odd_num) { print "$value is odd\n"; }
else { print "$value is even\n"; }

$value = 5;
$odd_num = $value % 2;
if ($odd_num) { print "$value is odd\n"; }
else { print "$value is even\n"; }

'hope this helps....




keep the rudder amid ship and beware the odd typo
 
You could compact that a bit:

my $value = 4;
if ($value%2) {print "$value is even";}
else {print "$value is odd";}
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Thanks a lot goboating and tom anderson,

with love to perl
Thendal
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top