Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Odd or Even Program Problem
#1
Hey Guys... Im having a problem with the code I coded.. Even If I input a even number it says it is Odd.. Here is what I got so far...

Code:
CLS
'Declarations
DIM number AS INTEGER
'End of Declarations

PRINT "Even or Odd Number"
PRINT
INPUT "Enter a number"; number

IF number / 2= 0 THEN

  PRINT "Number is Even"

ELSE
  PRINT "Number is Odd"
END IF
img]http://img213.imageshack.us/img213/6104/sig1jb.gif[/img]
Reply
#2
you should try MOD
.......
IF number MOD 2 = 0 then....
ormal service may never be resumed
Reply
#3
Could you care to explain the MOD function..

Thanks For that it works hehe...
img]http://img213.imageshack.us/img213/6104/sig1jb.gif[/img]
Reply
#4
What MOD does:
It gives the remainder after division. For example: 11 / 4 = 2 with remainder 3. So 11 MOD 4 is 3. It's short for Modulus, a fancy word for "divide these two numbers and just give me the remainder".

Why your code didn't work:
Most numbers, if you divide them by 2 the result won't equal zero. That's why it always says Odd. Enter 0 as the number and it will say Even.
Reply
#5
If you understand the AND operator, then try this:

if number AND 1 then print "Number is ODD"

The expression (number AND 1) will be true if the number has its one-bit set, otherwise the expression is false. If the one-bit is set, then the number must be odd.
*****
Reply
#6
awwwwww, you guys are to fast. i wanted to help the n00b. you guys are able to solve more harder problems and i can't. Cry oh well i try to be faster.

anyways,
Hybr!d, you could of solved the problem by running though the code manually
like this:
[syntax="qbasic"]IF number / 2= 0 THEN[/syntax]
what if we replace the number with 10?
[syntax="qbasic"]IF 10 / 2= 0 THEN[/syntax]
10 / 2 = 5. not 0
googly was right about MOD. MOD is pretty useful. you can even identify prime numbers using it.
quote="whitetiger0990"]whitetiger is.. WHITE POWER!!! [/quote]
Here
Reply
#7
Quote:googly was right about MOD.
googly? :rotfl:
Reply
#8
My usual advice is to create a function that returns the result of an AND operation with the number to check for ODDness and 1, as Moneo points out.

AND'ing a number with 1 returns true (-1) if the binary representation of the number has the 1st bit set (which all odd numbers do), and false (0) otherwise (for even numbers). This operation should be much faster than using the MOD operator.

Such a function might look like:

Code:
function IsOdd( x as integer ) as integer
   return x AND 1
end function

The advantages of using a function rather than not are two-fold:

1. You don't have to write "AND 1" over and over again, increasing the chances of typos, increasing the chances of bugs.
2. It makes your code much more clearer:
Code:
...
if( ( x*someNumber - ( y+1 ) ) AND 1 ) then ...
   ...
if( IsOdd( x*someNumber - ( y+1 ) ) then ...
   ...

The second IF statement is immediately clear in it's intent, while the first requires some hard thinking to determine what's going on - even then, are we checking for ODDness or a flag state (for a mathematical example like this, the answer is clear, but other cases the situation may not be so obvious)?
stylin:
Reply
#9
Quote:
Dio Wrote:googly was right about MOD.
googly? :rotfl:

http://forum.qbasicnews.com/profile.php?...ile&u=1818

Quote:you should try MOD

he was right about using the MOD function.
quote="whitetiger0990"]whitetiger is.. WHITE POWER!!! [/quote]
Here
Reply
#10
Dio:
Sorry, it didn't occur to me that was a username, I thought you were making a joke. :oops:

Quote:[...] returns true (-1) [...]
Minor correction: ANDing by 1 will never result in -1.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)