Python Program to Check Armstrong Number

Introduction

An Armstrong number (also known as 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. This tutorial will guide you through creating a Python program that checks whether a given number is an Armstrong number.

Example:

  • Input: 153

  • Output: 153 is an Armstrong number

  • Input: 123

  • Output: 123 is not an Armstrong number

Problem Statement

Create a Python program that:

  • Takes a number as input.
  • Checks if the number is an Armstrong number.
  • Displays whether the number is an Armstrong number or not.

Solution Steps

  1. Take Input from the User: Use the input() function to get a number from the user.
  2. Convert Input to Integer: Convert the input string to an integer using int().
  3. Calculate the Number of Digits: Find the number of digits in the input number.
  4. Calculate the Sum of Each Digit Raised to the Power of the Number of Digits: Iterate through each digit, raise it to the power of the number of digits, and sum the results.
  5. Check if the Sum Equals the Original Number: Compare the calculated sum to the original number.
  6. Display the Result: Use the print() function to display whether the number is an Armstrong number.

Python Program

# Python Program to Check Armstrong Number
# Author: https://www.rameshfadatare.com/

# Step 1: Take input from the user
num = int(input("Enter a number: "))

# Step 2: Calculate the number of digits
num_digits = len(str(num))

# Step 3: Calculate the sum of each digit raised to the power of the number of digits
sum_of_powers = sum(int(digit) ** num_digits for digit in str(num))

# Step 4: Check if the sum equals the original number
if sum_of_powers == num:
    print(f"{num} is an Armstrong number")
else:
    print(f"{num} is not an Armstrong number")

Explanation

Step 1: Take Input from the User

  • The input() function prompts the user to enter a number. The input is converted to an integer using int().

Step 2: Calculate the Number of Digits

  • The number of digits is calculated by converting the number to a string and using the len() function.

Step 3: Calculate the Sum of Each Digit Raised to the Power of the Number of Digits

  • The program iterates through each digit of the number, raises it to the power of the number of digits, and sums the results using a generator expression within the sum() function.

Step 4: Check if the Sum Equals the Original Number

  • The calculated sum is compared to the original number. If they are equal, the number is an Armstrong number.

Step 5: Display the Result

  • The print() function is used to display whether the number is an Armstrong number or not.

Output Example

Example 1:

Enter a number: 153
153 is an Armstrong number

Example 2:

Enter a number: 123
123 is not an Armstrong number

Example 3:

Enter a number: 9474
9474 is an Armstrong number

Conclusion

This Python program demonstrates how to check whether a number is an Armstrong number by calculating the sum of its digits raised to the power of the number of digits. It provides a clear example of using loops, mathematical operations, and conditionals in Python.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top