CREATE OWN LIBRARY

Armstrong Number

Back to Programming

Description

A number is said to be an Armstrong Number if the number is equal to the sum of its digits to the power the number of digits. That means, if the number has 3 digits then the number will be an Armstrong number if the number is equal to the sum of the cube of its digits.

Example:

153 is an Armstrong Number because the sum of its cube returns the number itself.

Here, the number of digits is 3.

13+53+33 = 1 + 125 + 27 = 153.

Similarly, if we consider the number 1634. Here, the number of digits is 4.

Therefore, 14 + 64 + 34 + 44 = 1 + 1296 + 81 + 256 = 1634.

1634 is also an Armstrong Number.

Let me consider the number 123. Here, the number of digits is 3.

13 + 23 + 33 = 1 + 8 + 27 = 36. Hence, the sum of the cube is not returning the original number. So, it is not an Armstrong Number.

Time Complexity:

while(m>0)-------------------------------------O(p)

{   c++;

m/=10;   }

//copying the value of n in m

m=n;

//finding the sum of the digits^number of digits

while(n>0)------------------------------------------O(p)

{              s=s+pow(n%10,c);

n=n/10;    }

The time complexity of this program is O(p) where ‘p’ is the number of digits of the given input.

Space Complexity:

The space complexity of this program is O(1) as it requires a constant number of memory space to execute the program for any given input.