+ 13
[challenge] check if a number is even without modulo operator or floor devision.
Modulo operator: 8/5 = 1 remainder 3 8%5 = 3 floor devision : 44//5 = 8.
33 RĂŠponses
+ 10
Ruby:
> 4.even?
=> true
+ 8
One-Liner:
https://code.sololearn.com/cvmYJZf28ivm/#cpp
+ 8
my java try
https://code.sololearn.com/c2GMyhQfPLmv/?ref=app
+ 5
https://code.sololearn.com/cC8FCmroHcN5/?ref=app
+ 4
@code learner
up to now the solutions are based on binary operators. I just ask myself if this "trick" works for the number 8.
+ 4
@alfred your code works for every number!
you perfectly hide a floor devision by using integer division.
This "trick" is not possible in every language
It is allowed and ... thumbs up for you!
+ 4
@vcc you solution counts as 1/2 - liner (-;
+ 3
@cool codin and @paul
perfect solution. Can you do it with again to check if a number is devisible by 8?
#Edit easy for all powers of 2- it is similiar
I have no solution for other numbers yet.
+ 3
print(["even","odd"][x&1])
+ 3
Check
this
out!!
https://code.sololearn.com/cnhmqfb9Irk3/?ref=app
+ 3
@infinity perfect!
now check devisible by 8 if you wanna
+ 3
here is the solution for 8... :D
https://code.sololearn.com/cAR135H32Zcq/?ref=app
+ 3
num & 1 ? "odd" : "even"
+ 3
I made this code in Python that checks if one number is divisible by another.
I tried to train with exceptions but inputs don't work well on the playground, so make sure to actually write an integer.
Oh, and the code is not really elegant, I would gladly accept some advice for that
https://code.sololearn.com/cMKqsWnL61Hb/?ref=app
+ 2
I saw the bitwise or operator today in a code from ++EviSolo and made use of it now (I am sorry it is not a oneliner):
https://code.sololearn.com/c0QEy4cmik6W/?ref=app
+ 2
My code works for 8 now too. Just by checking the 3 most right bits.
+ 2
Java -> int x = 10; return x/2*2 == x ? "Even" : "Odd";
Other language -> use truncate