For loop: Odd numbers turn 0 when checked if even
이전 댓글 표시
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
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!