Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Matlab Given a positive integer N, find the lowest multiple of N made up of only

ID: 3906052 • Letter: M

Question

Matlab

Given a positive integer N, find the lowest multiple of N made up of only 9’s and 0’s!

I need help debugging this matlab code! My code keeps running forever, it seems to be having a problem with the continue part.

% Lowest multiple with 9s and 0s

clc; clear;

N = input('Enter a positive whole number N: ','s');

cnt = 1;

while true

M = N*cnt;

while M >= 1

remainder = mod(10,M);

if remainder ~= 9 || remainder ~= 0

continue;

else

M = floor(M*10);

end

end

if M < 1

break;

end

cnt+1;

end

fprintf('Lowest multiple with only 9s and 0s: %d ',N)

Explanation / Answer

It keeps running in Infinite Loop Because Of


while M >= 1 --->It will never come out of Loop if M > 10

remainder = mod(10,M); ==>Lets say M = 11 , Then remainder is 10 , So M is multipliled by 10, it becomes 110

if remainder ~= 9 || remainder ~= 0

continue;

else

M = floor(M*10); ==> So M is multipliled by 10, it becomes 110

end

end

==================
1) You should change the above code, what are you trying to do ?
2) mod(10,M) means 10%M and NOT M%10


Let me know your doubts, I will help and make it work..Let me know what are you trying to do in comment