Python Program to check whether a given number is an Armstrong Number or not

Program

num = int(input("Enter the number:\t"))
temp = int(num)
Sum = 0
while temp != 0:
	digit  = temp % 10
	Sum  += digit ** 3
	temp = temp // 10
if num == Sum:
	print(num, "is an armstrong Number")
else:
	print(num, "is not an armstrong Number")

What is an Armstrong Number

An Armstrong number (also called a narcissistic number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits.

For a three-digit number, it follows this formula:
abc = a³ + b³ + c³

Examples

  • 153 = 1³ + 5³ + 3³ = 1 + 125 + 27 = 153 (Armstrong Number)
  • 9474 = 9⁴ + 4⁴ + 7⁴ + 4⁴ = 9474 (Armstrong Number)
  • 123 = 1³ + 2³ + 3³ = 1 + 8 + 27 = 36 (Not an Armstrong Number)

Python Program to Check Whether a Given Number is an Armstrong Number or Not

  1. Take user input

    • num = int(input("Enter the number:\t"))
    • The user enters a number, which is stored in num.
  2. Initialize variables

    • temp = num stores the original value for reference.
    • Sum = 0 is used to calculate the sum of cubes of digits.
  3. Extract and process digits using a loop

    • digit = temp % 10 extracts the last digit.
    • Sum += digit ** 3 adds the cube of the digit to Sum.
    • temp = temp // 10 removes the last digit by integer division.
  4. Check Armstrong condition

    • If num == Sum, print that the number is an Armstrong number.
    • Otherwise, print that it is not.

Key Takeaways

  • An Armstrong number is a number that equals the sum of its digits each raised to the power of the number of digits.
  • The program extracts each digit, cubes it, and sums the results.
  • Finally, it compares the sum with the original number to determine if it is an Armstrong number.

Output 1

Enter the number:	65
65 is not an armstrong Number

Output 2

Enter the number:	153
153 is an armstrong Number