Why does my output turn out like this certain times I run my code?

1 view (last 30 days)
Our FSE 100 teacher gave us this prompt:
"Show 'Mutually Prime' or 'Not Mutually Prime' if two numbers are inputted."
So I created this code, which seems to work perfectly fine:
a = input('Input first number: ');
b = input('Input second number: ');
if (a>b)
limit = b
end
if (b>a)
limit = a;
end
counter = 0;
for v = 2:limit
x = mod(a,v);
y = mod(b,v);
if (x == 0) && (y == 0)
counter = 1;
end
end
if (counter == 0 && a~=b)
disp('Mutually Prime');
else
disp('Not Mutually Prime');
end
And most of the time, I'm getting outputs I want, like this one below:
BUT sometimes I'm getting an output that looks like this:
This type of output occassionally shows up, and other times it doesn't, so I'm a little confused, especially since I'm new to MATLAB coding. Is this something MATLAB does, fluctuating the outputs like this? Or is there something wrong in my own code?
Basically, does anyone know if I can fix this so that, somehow, the "limit = ..." portion never shows up in the ouput?
ALSO another problem I'm having is when the two numbers inputted by the user are the same:
It's correct that they aren't mutually prime, but It's printing "100" twice, which I don't want. I can't seem to spot where the problem could be.
Thanks! :)

Answers (1)

Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam on 9 Oct 2020
in the first if statement, add a simi-colon ath the end:
if (a>b)
limit = b;
end
Also, you can simplify the first two if statements to:
limit = min(a,b);
  1 Comment
Kinza Rashid
Kinza Rashid on 9 Oct 2020
Thank you so much! That corrected the "limit = ..." problem, but is there a way to stop a duplicate being printed of the second variable if the two inputs are the same value (shown in the last picture)?

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!