Problem of the day
You are given an integer ‘NUM’ . Your task is to find out whether this number is an Armstrong number or not.
A k-digit number ‘NUM’ is an Armstrong number if and only if the k-th power of each digit sums to ‘NUM’.
Example153 = 1^3 + 5^3 + 3^3.
Therefore 153 is an Armstrong number.
The first line contains an integer ‘T’, which denotes the number of test cases to be run. Then, the ‘T’ test cases follow.
The first line of each test case contains a single positive integer, ‘NUM’.
Output Format:
For each test case, print ‘YES’ if it is an Armstrong number otherwise print ‘NO’.
The output of each test case will be printed in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 100
1 <= ‘N’ <= 10^9
Time Limit: 1 sec
1
13
NO
As 1^2 + 3^2 = 10 which is not equal to 13.So we can say it is not an Armstrong number.
1
371
YES
Initially find the number of digits in the number. Then find the sum of each digit to the power k and check if it is equal to the original number.
O(log10(N)), where N is the number given to you.
We just need to traverse the digits of the number.
O(1)
As we are using constant extra space.