Posts

Program for Armstrong Numbers

Given a number x, determine whether the given number is Armstrong's number or not. A positive integer of n digits is called an Armstrong number of order n (order is the number of digits) if

abcd... = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + ....

Here a, b, c and d are digits of input number abcd.....

Examples

Input: n = 153
Output: true
Explanation: 153 is an Armstrong number, 1*1*1 + 5*5*5 + 3*3*3 = 153

Input: n = 9474
Output: true
Explanation: 94 + 44 + 74 + 44 = 6561 + 256 + 2401 + 256 = 9474

Input: n = 123
Output: false
Explanation: 1³ + 2³ + 3³ = 1 + 8 + 27 = 36


// Input number

n = "153";   // Keep as string for iteration


// Get length of digits

len = n.length();


// Initialize sum

sum = 0;


// Loop through each digit

for each digit in n.toList("")

{

    num = digit.toNumber();

    sum = sum + num.power(len);

}


// Check Armstrong condition

if(sum == n.toNumber())

{

    info "True - Armstrong Number";

}

else

{

    info "False - Not an Armstrong Number";

}


Post a Comment