VOOZH about

URL: https://www.geeksforgeeks.org/python/python-program-to-check-armstrong-number/

⇱ Python Program to Check Armstrong Number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python Program to Check Armstrong Number

Last Updated : 29 May, 2026

Given a number n, the task is to check whether it is an Armstrong number or not. An Armstrong number is a number that is equal to the sum of its digits raised to the power of the total number of digits. For example:

Input: n = 153
Output: Armstrong Number
Explanation: 13 + 53 + 33 = 153

Let’s explore different methods to check Armstrong numbers.

Using a Mathematical Approach

This approach extracts digits using arithmetic operations and calculates the sum of digits raised to the required power. The final sum is then compared with the original number.


Output
Armstrong Number

Explanation:

  • len(str(n)) finds the total number of digits and t % 10 extracts the last digit of the number.
  • d ** p raises the digit to the required power and t //= 10 removes the last digit in each iteration.

Using String Conversion

This approach converts the number into a string and loops through each digit directly. Each digit is converted back to an integer for calculation.


Output
Armstrong Number

Explanation:

  • str(n) converts the number into a string and for d in str(n) accesses each digit one by one.
  • int(d) ** p raises each digit to the required power and sum() adds all powered digit values together.

Using map() and lambda

This approach uses map() and lambda to process each digit and calculate the Armstrong sum in a compact way.


Output
Armstrong Number

Explanation:

  • str(n) converts the number into a string and map() applies the lambda function to each digit.
  • lambda d: int(d) ** p raises each digit to the required power and sum() adds all calculated values together.

Using Recursion

This approach calculates the Armstrong sum recursively by processing one digit at a time until the number becomes 0.


Output
Armstrong Number

Explanation:

  • x % 10 extracts the last digit of the number and x // 10 removes the last digit in each recursive call.
  • The function keeps adding powered digits recursively. Recursion stops when the number becomes 0.
Comment
Article Tags:
Article Tags: