File Exchange

## isarmstrong(n)

version 2.0.0.0 (373 Bytes) by
Armstrong Numbers are those n-digit numbers, equal to the sum of the nth powers of its digits.

Updated 02 Mar 2016

Armstrong Numbers are having the property that an n-digit Armstrong Number will be equal to the sum of the nth powers of its digits. Examples are 0,1,2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, ...

### Cite As

Dr Raveendranathan K C (2021). isarmstrong(n) (https://www.mathworks.com/matlabcentral/fileexchange/48603-isarmstrong-n), MATLAB Central File Exchange. Retrieved .

Stephen Cobeldick

Neat. You could replace the |while| loop with |sprintf|: this is a more robust way of getting the digits. Also the final |if| statement is totally superfluous, the function really should just return the _logical_ value of |sumd==s|.

Just for fun, here is the same same functionality in just two lines:

>> fun = @(s)sum((s-'0').^numel(s));
>> isarm = @(n)n==fun(sprintf('%lu',n));
>> isarm(0)
ans =
1
>> isarm(1)
ans =
1
>> isarm(153)
ans =
1
>> isarm(154)
ans =
0

A MATLAB function to check whether a given number is an Armstrong Number or not.

##### MATLAB Release Compatibility
Created with R2014b
Compatible with any release
##### Platform Compatibility
Windows macOS Linux