Problem 26. Determine if input is odd
Solution Stats
Problem Comments

48 Comments
Added several more tests to limit hardcoded answers.
When they say return "true" they mean "1", and "false" means "0". Newbie mistake.
Add a couple of negative values to the test.
Test for a couple of negative values
Delete comment doesn't work!
I don't know what I am doing wrong. I tried it in Matlab and seems to work fine
y = rem(x,2);
if y == 1
disp('true')
else
disp('false')
end
it should be clearly specified that we should not type in the string true but if we give true to a variable then it takes it as 1
All solutions with score 10 use the regexp cheat.
I don't understand why my code isn't working?
if mod(n,2) == 1
tf = 'true'
else
tf = 'false'
end
function tf = is_odd(n)
if rem(n,2)== 0
tf = 'false'
else
tf = 'true'
end
end
why is this not working?
I bet all the solutions not using if else are all from engineering backgrounds with intimate knowledge of boolean logic
ipek: you only need one end.
Nolan: you aren't using the variables provided
eine interessante Frage...
Persistent server error trying to evaluate this solution...
Hello it's me
Nice one
Good one.
Nice problem
nice
good job
horrible problem
good
great
good problem
Great problem
great prob
Easy one.
kool
Easy enough that I can do it!
badge
easy
how to reduce size
i have the size of 28
Good Problem
Pretty Easy. Thanks!
Good use of the modulo operator. Took some quick presence of mind to realise how to use it, but it is definitely quick to crack.
Advice: Don't overthink it!
Thank you!
Nice and simple. Thanks for the points :)
Thank you!
Adding comment for testing badges!!
Adding comment for testing badges!!
Solution stats show around 2/3rd of the attempts were wrong. Shows improtance of reading the problem well.
This is my first programming problem that I have written in c language.
Nice!
This was nice
Good Problem!
10/10 would code again
Not to hard
Solution Comments

1 Comment
Is there any other way of finding mod in MATLAB other than using the mod(a,b) function?

2 Comments
not difficult
pretty easy

1 Comment
hard

1 Comment
This test was a good refresher on if/else statements.

1 Comment
this is a great problem to practice true/false

2 Comments
very easy
good

1 Comment
Nice one!

1 Comment
Adding comment for testing badges!!

2 Comments
What exactly the error is
function tf = is_it_odd(n)
if
rem(n,2)==0
tf = 'false';
else
tf='true';
end
end
end
where is the problem

1 Comment
whyyyyy

1 Comment
fun problem, however, smallest solutions are broken for all problems as they use ! & regexp

1 Comment
great problem

1 Comment
what is wrong here?

1 Comment
help aha?

2 Comments
come on really? whats wrong with this?
You need to assign your answer to the output variable tf.

1 Comment
...

4 Comments
why is this solution incorrect?
I think there is a problem with the Cody server since yesterday.
Apologies for the down time. It should be working now
You actually don't need the Boolean assignments, technically a 0 and a 1 as the output will be good enough

1 Comment
so easy

2 Comments
I have tried many times this one. It runs fine but I am not able to understand why is it showing as wrong solution.
Can anybody help on this?
Remove the single quotes in true/false. It might work. ☺

2 Comments
function tf = is_it_odd(n)
if mod(n,2)==0
tf = 'false';
else
tf = 'ture';
end
why assertion failed. it works in matlab.
Thanks for your answer, you had a typo in true, also you should remove ' ' from false and true

1 Comment
very easy

1 Comment
good

1 Comment
easy

1 Comment
good job

2 Comments
good
good job

2 Comments
gj
good job

2 Comments
too demanding
good job

3 Comments
my solution was right and i tested it i m not sure what is going on
function [tf] = is_it_odd(n)
if rem(n,2)>0
tf='true';
else
tf='false';
end
end
my answer correct
good job

3 Comments
How can be a smaller size? tf = rem(n,2)
good job
I don't think that simply using tf=rem(n,2) should be allowed, its implicit casting which isn't a good thing to depend on.

2 Comments
Had cofused mod with % in python
good job

2 Comments
What is the problem here?
good job

2 Comments
this work on matlab but not here, why?
good job

3 Comments
My code works but it fails the test suite despite it giving the correct output. What's the deal?
true, not 'true', false, not 'false'
good job

1 Comment
Assertation failed.
Code works perfectly on my computer.
Must be a bug with this particular problem.

4 Comments
It seems to provide the desired solution. Could anyone tell me why this is an incorrect solution?
The output should be logical,and it should't be char type:
function tf = is_it_odd(n)
x = n/2;
y = floor(x);
if x == y
tf = false;
else
tf = true;
end
end
good job
good solution

3 Comments
Does anyone know why this doesn't answer the problem? When worked out in matlab it works just fine.
1. do not use "input(...)", "n" is defined variable and input doesn't return anything. 2. "=" is used for assignments, not for comparisions. This one returns syntax error in line 3 (if ...), use "==" instead. 3. Output should be logical, not string. Use "false" instead of "'false'" etc. 4. Return the output via variable specified for it in first line of the function, in this case it is "tf". Do not use "fprintf" for that. 5. Read The Friendly Manual. Just type "doc" in Command Window  there are tons of documentation, tutorials.
good job

2 Comments
This solution works offline?
good job

10 Comments
How does this help anyone? This has nothing to do with learning better MATLAB code and doesn't even run.
This is not useful. What a waste of time...!
Can you please explain how that solution work? thanks
On the positive side of it, I'm very happy not to see people blindly copy pasting the above solution for every problem. This shows that the majority of the participants actually understand why we're all doing this. BTW, smart workaround!
Hi Ali, what is the workaround your speaking of? I am very confused by this solution, but I would like to understand it.
Even though it doesn't solve the actual problem, I thought this solution was clever enough (without too many copypastes by others, or maybe MathWorks fixed this workaround since then) to merit explaining it.
For those wondering, the line of code within the is_it_odd function does the following:
1. It does a system call to echo a single line to the standard i/o (a virtual place where programs can share/exchange their inputs and outputs).
2. The single line is in the form of a Matlab inline function, named assert(), that blackholes any inputs and always outputs the boolean 'true.'
3. The system call takes this single inline Matlab function and replaces the contents of assert.m (of whatever structure/setup for Matlab interpretation that Cody uses) with that single line.
4. Cody checks solutions with a series of assert(isequal(stuff,things)) calls, which now always evaluate to be true in this particular instance, as opposed to actually comparing if 'stuff' and 'things' are the same.
good job
Nice work on a creative solution that has the "smallest size" per Cody's grading criteria. However, as a beginner to MATLAB, I don't see why I would want to write something this long/complex when there are much shorter solutions (perhaps not per Cody's criteria but they use less characters). I guess it's all about winning the contest.
want to how can be size 9
9...............how can you do that......

2 Comments
I tried this in my pc, all results appear as it is in your ans_correct.
good job

3 Comments
what is wrong with the solution?
You're returning the string "true" or "false" which the test suite is trying to compare to a boolean variable true or false. Since the string does not equal the boolean value, assert fails.
good job

2 Comments
% i mean that: test = is_it_odd; :D
good job

2 Comments
Why is function "odd" not working?
good job

3 Comments
Is this code wrong?
tf=floor(n/2)*2==n
yup
good job

2 Comments
How could i get this any shorter?
good job

3 Comments
Why is this solution not working?? works on my matlab 2014 version!!
I think the ' ' characters might be the problem.
good job

2 Comments
it should use mod.
good job

2 Comments
realized my true/false assignments were off. They should be reverse...if mod(n,2) ~= 0; tf=true; else tf = false; end
good job

1 Comment
can anyone tell me better solution than this?

1 Comment
Well done brother :)

1 Comment
Suggested Solutions reminds me lambda expressions in C++ or Java. And i agree regexp misusage is truly hiding the code in MATLAB build in functions. The size = 10 is misleading, since unwrapping the if condition, can never be lesser than a single line function, like this
function tf = is_it_odd(n)
tf = mod( n, 2 ) == 1;
end
Obviously no if environment is called and an explicitly assignment is in this case totally not needed, hence mod is returning a value (type: @logical).
Further this expression cannot work without any explicit assignment command for the output result.
My strict recommendation for MATLAB newbies to ignore such statements. It is truly not good practice and me as Software Developer would directly reject such solutions.

1 Comment
function tf = is_it_odd(n)
mod(n,2);
end

1 Comment
This is called cheating....Even this code is failed

1 Comment
for 21 size. little editing :)
function ans = is_it_odd(n)
if mod(n,2)==0; false;
else
true
end

1 Comment
bugger

1 Comment
I think the assertion is flawed, because
isequal(is_it_odd(2),true) == true == 1
returns
ans = 1

1 Comment
Here, false and 0 (as double) mean same.
Also true and 1 (as double) mean same.

1 Comment
what the matter,such an easy problem prevent me from continuing solving the following problems,so why,l can't accept the format of the solver function,it is so stupid,it is not flexible,l can't stand

1 Comment
this returns "1" & not "true", but the validation for this cody problem is spurious since isequal(true,1) is actually true (when it should really throw an error).

2 Comments

2 Comments
Seems to me that the "best" answer would be tf = logical(mod(n,2)). But I guess the description does not explicitly require logicals, so maybe I am reading too much into the problem. Using logical makes the solution 17 compared to 13 without.
by calling logical function you add extra code.

2 Comments
I'am bad!
I don't think you know what odd/even means.
odd = 1 3 5 ...
even = 2 4 6 ...

1 Comment
I know this isn't correct, only submitted it because I couldn't figure out how it was possible to solve the problem with code size 12, and wanted to see the solution, only to find out that the 'leading solution' was wrong.

3 Comments
Does it work if n=3 or greater??
Pretty confusing!!
Ofcourse not, the problem with cody is that you can see the unit test.
But the good thing about Cody is that you can update the test suite. Thanks for the comments, and please keep flagging bad tests so we can fix them.

1 Comment
This doesn't return a logical 'true' or 'false'.

1 Comment
Persistent server error trying to evaluate this solution...

1 Comment
Hooray for ambiguous classing requirements.
How about this for the "best" solution:
x_odd = x(is_it_odd(x)); % :/
The answer should be logical(mod(n,2)).
Problem Recent Solvers20979
Suggested Problems

Project Euler: Problem 5, Smallest multiple
860 Solvers

801 Solvers

Rotate input square matrix 90 degrees CCW without rot90
433 Solvers

425 Solvers

2658 Solvers
More from this Author95
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!