# 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.

${1}^{3}+{5}^{3}+{3}^{3}$  = 1 + 125 + 27 = 153.

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

Therefore, ${1}^{4}+{6}^{4}+{3}^{4}+{4}^{4}$ = 1 + 1296 + 81 + 256 = 1634.

1634 is also an Armstrong Number.

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

${1}^{3}+{2}^{3}+{3}^{3}$ = 1 + 8 + 27 = 36. Hence, the sum of the cube is not returning the original number. So, it is not an Armstrong Number.

### Algorithm

INPUT: A number

OUTPUT: Whether it is an Armstrong Number or not.

PROCESS:

Step 1: [Taking the input]

Read n [the number to be checked]

Step 2: [Checking for an Armstrong Number]

Set c<-0

Set s<-0

Set m<-n

[Counting the digits of the number]

While m>0 repeat

Set c<-c+1

Set m=m/10

[End of ‘while’ loop]

Set m<-n

[Calculating the sum of the powers]

While m>0 repeat

Set s<-s + (m mod 10) c

Set m=m/10

[End of ‘while’ loop]

[Checking for an Armstrong Number]

If n=s then

Print “Armstrong Number”

Else

Print “Not an Armstrong Number”

[End of ‘if’]

## 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.