File Exchange

image thumbnail

isarmstrong(n)

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

1 Download

Updated 02 Mar 2016

View Version History

View License

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 .

Comments and Ratings (2)

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

Community Treasure Hunt

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

Start Hunting!