1. Create a new function in MATLAB. 2. Title this function IsOdd. This function
ID: 3692764 • Letter: 1
Question
1. Create a new function in MATLAB. 2. Title this function IsOdd. This function will take an integer input and produce a string “yes” or “no” output. 3. Utilizing if/else statements and for loops – create a function that can determine if a number is odd as opposed to even. HINT: an odd number has a remainder when divided by 2 (function rem() might be helpful) 4. Comment your function 5. Call your function in the workspace using numbers 1 through 4. What answers do you expect? Is your function working properly? Take a screen shot of all four outputs. 6. Now – test your function on the number zero. Does your function work properly? Modify your function to handle the number zero by outputting a string ‘That is quite a conundrum!’ (Even though zero is an even number). Screen shot your output in the workspace.
Explanation / Answer
function str = new IsOdd(num)
str=NULL;
if num < 1
error('That is quite a conundrum!')
if(rem(num,2)==0)
str='no'
else
str='yes'
end
>> IsOdd(1)
yes
>>IsOdd(2)
No
>>IsOdd(3)
yes
>>IsOdd(4)
no
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.