Array ( [0] => [1] => questions [2] => Basic [3] => Armstrong-Number ) Basic | Armstrong Number | THE INQUISITIVE

Armstrong Number

Description

Given a integer N. You have to find the given number is Armstrong number or not. (Armstrong number is a number which is the sum of power of length of a given number to every digit is equal to given number).

Input Format

First line contains a integer N.

Output Format

Print Armstrong if it is Armstrong number else print not Armstrong.

Example 1:

Input
`153`
Output
`Armstrong`
Example 2:

Input
`145`
Output
`Not Armstrong`
Example 3:

Input
`53`
Output
`Not Armstrong`

Approach

Approach 1:
Step-i) convert given number to string
Step-ii) calculate length of the string to get no of digits
Step-iii) initialize a variable to 0
Step-iv) get the remainder by dividing with 10
Step-v) raise the remainder to the power of length and add it to the variable
Step-vi) divide the number with 10
Step-vii) repeat steps iii, iv, v till the number is greater than 0
Step-viii) check whether the sum and the given number are same or not
Step-ix) if same return "Armstrong number" else return "Not Armstrong"

Time Complexity: O(n)+O(log(n)*log(log(n)))
O(n) for getting length of string, log(n) for traversing , for power function it is log(log(n)). It can be rounded to O(n)
Space Complexity: O(1)

Approach 2:

Step-i) use log function to get the length of the number
Step-ii) initialize a variable to 0
Step-iii) get the remainder by dividing with 10
Step-iv) raise the remainder to the power of length and add it to the variable
Step-v) divide the number with 10
Step-vi) repeat steps iii, iv, v till the number is greater than 0
Step-vii) check whether the sum and the given number are same or not
Step-viii) if same return "Armstrong number" else return "Not Armstrong"

Time Complexity: O(log(n)*log(log(n)))
log(n) for traversing , for power function it is log(log(n))

Space Complexity: O(1)

Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.