For loop: Odd numbers turn 0 when checked if even
Show older comments
Hello,
Sorry if the title is misleading. In short, when code checks if number is even; it assigns it to evens. However, if number is odd; it turns it into 0 and assigns it to even.
Here is the output...
48 55 6 66 89 11 44 29 99 61 26 14 55 83 84 84 21 55 88 13
48 0 6 66 0 0 44 0 0 0 26 14 0 0 84 84 0 0 88
48 is even, 55 is not so it turns into 0, 6 is even, and so on....
integers = randi([1 100], 1, 20);
evens = ones(1);
odds = ones(1);
for k = 1:length(integers)
if mod(integers(k), 2) == 0 % check if number is even
evens(k) = integers(k);
end
odds(k) = integers(k); % else if number is odd
end
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!