Be the first user to complete this post
|
Add to List |
411. Check if the given number is Armstrong number or not
Objective: Given a number, write a program to find out whether the number is Armstrong number or not
What is Armstrong number: Determine the number of digits in the number. Call that n. Then take every digit in the number and raise it to the n power. Add all those together, and if your answer is the original number then it is an Armstrong number.
Example:
N = 123
digits = 3
13+23+33=36
123 is not Armstrong number.
Output: false N = 153
digits = 3
13+53+33=153
153 is an Armstrong number.
Output: true N = 1634
digits = 4
14+64+34+44=1634
1634 is an Armstrong number.
Output: true N = 2356
digits = 4
24+34+54+64=2018
2018 is not an Armstrong number.
Output: false
Approach:
- Get all the digits in the number.
- Let's say the count of digits is x.
- Iterate through digits and calculate sum= digit1x +digit2x+.......+digitxx.
- check if the given number is equal to the sum, means the number is Armstrong, return true else number is not Armstrong, return false.
Output:
Number 123 is Armstrong Number: false Number 153 is Armstrong Number: true Number 1634 is Armstrong Number: true Number 2356 is Armstrong Number: false