LOWEST MULTIPLE WITH 9s and 0s
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
% Lowest multiple with 9s and 0s
clc; clear;
N = input('Enter a positive whole number N: ');
cnt=1;
while true
cnt = cnt+1;
M = N*cnt;
while M >= 1
remainder = mod(M,10);
if remainder == 0 && remainder == 9
continue;
else
M = floor(M/10);
end
end
if M < 1
break;
end
end
fprintf('Lowest multiple with only 9s and 0s: %d \n', (N*cnt));
I am trying to find the lowest multiple of a number which is only with 9s and 0s. When I input 5 the program outputs 10 and I can't see where my mistake is.
0 commentaires
Réponses (1)
Geoff Hayes
le 14 Fév 2019
Spiro - consider the line of code
if remainder == 0 && remainder == 9
Your remainder cannot be both 0 and 9...it should be one or the other. Try changing this to
if remainder == 0 || remainder == 9
to see what happens. Also, I think that if the remainder is zero or nine then this is telling you that your right-most digit is either zero or nine. If this is true, then you want to consider the next right-most digit which I think that you are trying to obtain with the line
M = floor(M/10);
So you want reduce your M if and only if the remainder is 0 or 9. If the remainder is something else, then you want to "start again" and so increment cnt and then obtain a new M.
Also, you may want to initialize cnt to zero (instead of one) to handle the case where the input is 9 (no reason to consider multiples of this number).
0 commentaires
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!