Armstrong Number in Python: Check with Easy Program

Armstrong Number in Python: Check with Easy Program

Armstrong Number in Python | Python Program to Check Whether the Given Number is Armstrong or Not

In this article, we will explore various methods to check whether a given number is an Armstrong number using Python. Along the way, we will also discuss the concept of Armstrong numbers and implement Python programs to verify them.

What is an Armstrong Number?

Armstrong Number in PythonAn Armstrong number (also known as a narcissistic number) is a number that equals the sum of its own digits raised to the power of the number of digits.For example:
  • 153 is an Armstrong number because 13+53+33=1531^3 + 5^3 + 3^3 = 153.
  • 9474 is an Armstrong number because 94+44+74+44=94749^4 + 4^4 + 7^4 + 4^4 = 9474.

Input and Output Format

Input Format:

  • Input consists of a single integer.

Output Format:

  • Output is a single line stating:
    • “Armstrong Number” if the input is an Armstrong number.
    • “Not an Armstrong Number” otherwise.

Sample Input and Output

Sample Input:

153

Sample Output:

Armstrong Number

Sample Input:

9475

Sample Output:

Not an Armstrong Number

Algorithm to Check Armstrong Number

Steps:

  1. Get the input number from the user.
  2. Calculate the total number of digits in the number.
  3. Compute the sum of each digit raised to the power of the total number of digits.
  4. Compare the computed sum with the original number:
    • If they are equal, print “Armstrong Number”.
    • Otherwise, print “Not an Armstrong Number”.

Method 1: Using the math Module

We can use Python’s math module to efficiently calculate the power of digits.
from math import pow

# Function to check Armstrong number
def is_armstrong(number):
    result = 0
    n = len(str(number))  # Number of digits
    temp = number

    while temp != 0:
        remainder = temp % 10
        result += pow(remainder, n)
        temp //= 10

    return result == number

# Input from user
number = int(input("Enter the number: "))
if is_armstrong(number):
    print("Armstrong Number")
else:
    print("Not an Armstrong Number")

Input:

153

Output:

Armstrong Number

Method 2: Using a While Loop

Using a While LoopThis method avoids using external modules and calculates the power manually.
# Program to check Armstrong number using a while loop
number = int(input("Enter the number: "))
sum = 0
temp = number
n = len(str(number))

while temp > 0:
    digit = temp % 10
    sum += digit ** n
    temp //= 10

if number == sum:
    print("Armstrong Number")
else:
    print("Not an Armstrong Number")

Input:

9474

Output:

Armstrong Number

Method 3: Finding Armstrong Numbers in a Range

This method prints all Armstrong numbers within a given range.
# Program to find Armstrong numbers within a range
lower = int(input("Enter the lower range: "))
upper = int(input("Enter the upper range: "))

for num in range(lower, upper + 1):
    order = len(str(num))
    sum = 0
    temp = num

    while temp > 0:
        digit = temp % 10
        sum += digit ** order
        temp //= 10

    if num == sum:
        print(num, end=" ")

Input:

100
500

Output:

153 370 371 407

Visual Representation

  • Diagram: A step-by-step flowchart of how Armstrong numbers are calculated.
  • Graph: A comparison of Armstrong numbers within a range versus their respective sums.

Suggested Visuals:

  1. Diagram: Flowchart explaining the Armstrong number algorithm.
  2. Table: Side-by-side comparison of digits, their powers, and the resultant sum for a sample input.

Conclusion

Armstrong numbers are an interesting mathematical concept that can be implemented in Python with ease. Whether you’re a beginner or an experienced programmer, understanding and coding this logic is a great way to enhance your Python skills. Try these methods and challenge yourself to find Armstrong numbers in different ranges. Armstrong Number in Python

c