LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-12-2008, 06:09 AM   #1
B-Boy
Member
 
Registered: Jan 2008
Posts: 103

Rep: Reputation: 15
test for odd number


Hi guys I need help again...how can i check if a var is an odd number
 
Old 08-12-2008, 06:24 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
In any particular language, or a general method? In general you do a mod 2 and see if the result is 0 or 1.
 
Old 08-12-2008, 06:30 AM   #3
B-Boy
Member
 
Registered: Jan 2008
Posts: 103

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by matthewg42 View Post
In any particular language, or a general method? In general you do a mod 2 and see if the result is 0 or 1.
in bash scripting
 
Old 08-12-2008, 06:48 AM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Try this:
Code:
if [ $(echo "$value%2" | bc) -eq 1 ]; then
  echo "$value is an odd number"
fi
ta0kira
 
Old 08-12-2008, 07:35 AM   #5
B-Boy
Member
 
Registered: Jan 2008
Posts: 103

Original Poster
Rep: Reputation: 15
Smile

Quote:
Originally Posted by ta0kira View Post
Try this:
Code:
if [ $(echo "$value%2" | bc) -eq 1 ]; then
  echo "$value is an odd number"
fi
ta0kira
thanks that is exactly what i needed
 
Old 08-12-2008, 07:35 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Running bc only to see if a number is od or even looks a bit like an overkill to me.

Faster version:
Code:
if [ $(($value%2)) == 0 ]; then
    echo Even
else
    echo odd
fi
 
Old 08-12-2008, 09:54 PM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
The "traditional" way I've seen it done is by a logic-AND with $01. This tests the least-significant bit: zero means even.
Code:
if (foo & 0x01) {
  // number is odd
} else {
  // number is even
}
 
Old 08-13-2008, 12:22 AM   #8
B-Boy
Member
 
Registered: Jan 2008
Posts: 103

Original Poster
Rep: Reputation: 15
thanks guys
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
test number within a range tostay2003 Programming 2 12-21-2007 08:42 AM
'magic' number - when do programs test for it? RedOctober45 Linux - Software 3 10-24-2007 10:25 PM
Bash: how to test for a number in Array within if loop? realos Programming 1 12-15-2006 07:59 AM
odd problem with fedora test 2 etjazz Linux - Networking 6 04-05-2004 03:50 AM
why there is a need for minor number and major number for monitor/keyboard in pc? tripathi Solaris / OpenSolaris 1 11-07-2003 09:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration