GeeksforGeeks App
Open App
Browser
Continue

# Python Program to check Armstrong Number

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

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

Example:

Input : 153
Output : Yes
153 is an Armstrong number.
1*1*1 + 5*5*5 + 3*3*3 = 153

Input : 120
Output : No
120 is not a Armstrong number.
1*1*1 + 2*2*2 + 0*0*0 = 9

Input : 1253
Output : No
1253 is not a Armstrong Number
1*1*1*1 + 2*2*2*2 + 5*5*5*5 + 3*3*3*3 = 723

Input : 1634
Output : Yes
1*1*1*1 + 6*6*6*6 + 3*3*3*3 + 4*4*4*4 = 1634

## Python

 # Python program to determine whether# the number is Armstrong number or not # Function to calculate x raised to# the power ydef power(x, y):         if y == 0:        return 1    if y % 2 == 0:        return power(x, y // 2) * power(x, y // 2)             return x * power(x, y // 2) * power(x, y // 2) # Function to calculate order of the numberdef order(x):     # Variable to store of the number    n = 0    while (x != 0):        n = n + 1        x = x // 10             return n # Function to check whether the given# number is Armstrong number or notdef isArmstrong(x):         n = order(x)    temp = x    sum1 = 0         while (temp != 0):        r = temp % 10        sum1 = sum1 + power(r, n)        temp = temp // 10     # If condition satisfies    return (sum1 == x) # Driver codex = 153print(isArmstrong(x)) x = 1253print(isArmstrong(x))

Output

True
False

Time complexity: O((logn)2)

Auxiliary Space: O(1)

Method: A positive integer is called an Armstrong number if an Armstrong number of 3 digits, the sum of cubes of each digit is equal to the number itself. For example, 153 is an Armstrong number because
153 = 1*1*1 + 5*5*5 + 3*3*3
The while loop iterates like first, it checks if the number is not equal to zero or not. if it is not equal to zero then enter into the loop and find the reminder of number ex: 153%10 gives reminder 3. In the next step add the cube of a number to the sum1(3*3*3). Then the step gives the quotient of the number (153//10=15). this loop will continue till the given number is equal to zero.

## Python3

 # python 3 program# to check whether the given number is armstrong or not# without using power function n = 153  # or n=int(input()) -> taking input from users = n  # assigning input value to the s variableb = len(str(n))sum1 = 0while n != 0:    r = n % 10    sum1 = sum1+(r**b)    n = n//10if s == sum1:    print("The given number", s, "is armstrong number")else:    print("The given number", s, "is not armstrong number") # This code is contributed by Gangarajula Laxmi

Output

The given number 153 is armstrong number

Please refer complete article on Program for Armstrong Numbers for more details!

Time complexity: O(logn)

Auxiliary Space: O(1)

#### Approach#3: Using string manipulation

This approach involves converting the input number into a string and iterating through each digit in the string. For each digit, we raise it to the power of the number of digits in the input number, and sum up the results. If the final sum equals the input number, it is an Armstrong number.

#### Algorithm

1. Convert the input number into a string using str(num).
2. Find the length of the string using len(num_str) and store it in n.
3. Initialize a variable sum to zero.
4. Iterate through each digit in the string using a for loop, and convert each digit back to an integer using int(digit).
5. Raise each digit to the power of n using int(digit)**n, and add the result to sum.
6. After the loop is complete, check whether sum is equal to num.
7. If sum is equal to num, return True (the input number is an Armstrong number).
8. If sum is not equal to num, return False (the input number is not an Armstrong number).

## Python3

 def is_armstrong(num):    num_str = str(num)    n = len(num_str)    sum = 0    for digit in num_str:        sum += int(digit)**n    if sum == num:        return True    else:        return Falsenum=153print(is_armstrong(num))

Output

True

Time complexity: O(n), wheer n is length of number
Auxiliary Space: O(1)

### Method: Digit by digit sum approach to check for Armstrong number

1. Find the number of digits in the given number.
2. Initialize a variable sum to zero.
3. Extract each digit from the given number and raise it to the power of the number of digits and add it to the sum.
4. If the sum is equal to the given number, then it is an Armstrong number, else it is not. #include <iostream>

## Python3

 import math def isArmstrong(num):    n = num    numDigits = 0    sum = 0         # Find number of digits in num    while n > 0:        n //= 10        numDigits += 1         n = num         # Calculate sum of digits raised to the power of numDigits    while n > 0:        digit = n % 10        sum += math.pow(digit, numDigits)        n //= 10         # Check if num is Armstrong number or not    if sum == num:        return True    return False # Example 1num1 = 1634if isArmstrong(num1):    print(num1, "is an Armstrong number.")else:    print(num1, "is not an Armstrong number.") # Example 2num2 = 120if isArmstrong(num2):    print(num2, "is an Armstrong number.")else:    print(num2, "is not an Armstrong number.")

Output

1634 is an Armstrong number.
120 is not an Armstrong number.

Time Complexity: O(log n), where n is the given number.
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up