+ 1
How do I determine an odd number in python?
`if x %2 == 0:` does it for even but what would include 1 using modulo as an odd number?
9 Respostas
0
If x%3 == 0:
print("odd")
Else:
Print("even")
+ 4
Taking the Waqar's code:
Suppose x = 6
Then 6 % 3 = 0 but it would print "odd".
Suppose x = 7
Then 7 % 3 = 1 but it would print "even".
(edited)
+ 4
ρү૨œdԌ૨ × if x % 2 == 0 is even
then...
x % 2 != 0 would be odd
Of course you will have to determine how you want to classify 0
+ 2
Even: If you can express the number as 2*k where k is an integer, then its remainder wgen divided by 2 is 0
Odd: 2*k + 1
So because (2*k + 1) % 2 != 0, it's odd. (edit: the divisor is 2, the quotient is k, remainder is 1)
So you're still using mod 2. There's no mod 1 if that’s what you're asking.
adding 1 makes an odd num even:
(2*k + 1) + 1 = 2*k + 2 = 2*(k +1)
---------
Mods:
----------
if x is equivalent to c in mod a
Then in the decimal system (base 10),
x = ka + c where the quotient k is a natural number (N = {0, 1, 2,...}), such that:
if mod 3:
x =~ 0 if x = {0, 3, 6,...} remainder 0 if divided by 3 aka multiples of 3
x =~ 1 if x = {1, 4, 7,...} rem 1
x =~ 2 if x = {2, 5, 8,...} rem 2
So if you have mod x the largest integer in mod x is x-1
So, if 1 is the largest number you can use, you are dealing with mod(2).
You cannot have mod 1, because no number will have a remainder when divided by 1, it'd be just zero. Every number would be equivalent to zero, so long as they are integers.
+ 1
Lisa so 0 is odd? that would more sense than it being even but less sense than it being zero... OH i see. Its less false.
+ 1
ρү૨œdԌ૨ ×
The number which is divided by 2 (reminder 0) is a Even number otherwise Odd number (reminder 1)
0
Alternative to "if x % 2 != 0":
If x % 2 > 0
- 1
Waqar Ahmed thank you 🤖
- 1
if x%2== 0:
print("odd")
else:
print("even")
If a number divided by 2 and remainder become 0 then it will an even number .