Open In App

Python Program to Check Armstrong Number

Improve
Improve
Like Article
Like
Save
Share
Report

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

Python Program to check Armstrong Number

Python




# Python program to determine whether
# the number is Armstrong number or not
 
# Function to calculate x raised to
# the power y
def 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 number
def 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 not
def 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 code
x = 153
print(isArmstrong(x))
 
x = 1253
print(isArmstrong(x))


Output

True
False

Time complexity: O((logn)2)

Auxiliary Space: O(1)

Python program to check if a number is an Armstrong number without using the power function

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 user
s = # assigning input value to the s variable
b = len(str(n))
sum1 = 0
while n != 0:
    r = n % 10
    sum1 = sum1+(r**b)
    n = n//10
if 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)

Python program to check if a number is an Armstrong number 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 False
num=153
print(is_armstrong(num))


Output

True

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

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 1
num1 = 1634
if isArmstrong(num1):
    print(num1, "is an Armstrong number.")
else:
    print(num1, "is not an Armstrong number.")
 
# Example 2
num2 = 120
if 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)

Python program to check if a number is an Armstrong number in return statement

Here’s a Python program to check if a number is an Armstrong number using just 3 lines of code:

Python3




def is_armstrong_number(number):
    return sum(int(digit)**len(str(number)) for digit in str(number)) == number
 
 
# Example usage:
num = 153
if is_armstrong_number(num):
    print(f"{num} is an Armstrong number")
else:
    print(f"{num} is not an Armstrong number")


Output

153 is an Armstrong number


Last Updated : 23 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads