0
int = input('');
l=numel(int2str(int));
t=num2str(int);
add = 0;
for i=1:l
    add = add + str2double(t(i))^l;
end
if add == int 
    disp(int)
else
    armstrong = int+1;
    result = 0;
    while armstrong~=result
        result = 0;
        for j = 1:l
            result = result + str2double(t(j))^l;
        end
        if result == armstrong
            disp(armstrong)
        else
            armstrong = armstrong+1;
        end
    end
end

Here is my code to find the armstrong number greater than the inputted number, but the code is not displaying the armstrong number. I could not identify the problem. The while loop does not stop and variable 'armstrong' gets greater and greater. Why could this be?

Wolfie
  • 27,562
  • 7
  • 28
  • 55
  • It looks like you're using `==` to compare a floating-point value, which will almost always fail. See https://stackoverflow.com/questions/23824577/what-are-the-best-practices-for-floating-point-comparisons-in-matlab – beaker Oct 05 '22 at 14:22
  • ... and https://stackoverflow.com/questions/686439/why-is-24-0000-not-equal-to-24-0000-in-matlab . You should compare the values within some absolute tolerance rather than absolute equality – Wolfie Oct 05 '22 at 15:30

0 Answers0