Python Program to Check Armstrong Number

The provided Python program is designed to determine whether a given number is an Armstrong number or not. An Armstrong number is a special type of number where the sum of its individual digits, each raised to the power of the total number of digits in the number, equals the original number itself. The program begins by prompting the user to input a number. It then converts the number to a string to calculate the number of digits and subsequently computes the sum of each digit raised to the power of the total number of digits. The program finally compares this sum with the original number and prints a message indicating whether the entered number is an Armstrong number or not. This program serves as a concise and illustrative example of how to implement a basic Armstrong number checker in Python.


Source code

def is_armstrong_number(number):
    # Convert the number to a string to find the number of digits
    num_str = str(number)
    
    # Find the number of digits
    num_digits = len(num_str)
    
    # Calculate the sum of digits raised to the power of the number of digits
    armstrong_sum = sum(int(digit) ** num_digits for digit in num_str)
    
    # Check if the sum is equal to the original number
    return armstrong_sum == number

# Input from the user
user_number = int(input("Enter a number: "))

# Check if the entered number is an Armstrong number
if is_armstrong_number(user_number):
    print(f"{user_number} is an Armstrong number.")
else:
    print(f"{user_number} is not an Armstrong number.")

Description

Certainly! Let’s break down the provided Python code step by step:

  1. Function Definition:
   def is_armstrong_number(number):

The program defines a function named is_armstrong_number that takes an integer number as an argument. This function will determine whether the given number is an Armstrong number.

  1. Convert Number to String:
   num_str = str(number)

The integer input is converted to a string (num_str). This conversion allows us to easily iterate through individual digits.

  1. Find the Number of Digits:
   num_digits = len(num_str)

The program calculates the number of digits in the input number by finding the length of the string representation.

  1. Calculate Armstrong Sum:
   armstrong_sum = sum(int(digit) ** num_digits for digit in num_str)

Using a generator expression, the program calculates the sum of each digit raised to the power of the total number of digits. The sum function aggregates these values.

  1. Check for Armstrong Number:
   return armstrong_sum == number

The function returns True if the calculated sum is equal to the original number, indicating that the input is an Armstrong number; otherwise, it returns False.

  1. User Input:
   user_number = int(input("Enter a number: "))

The program prompts the user to enter a number, and the input is converted to an integer.

  1. Check and Print Result:
   if is_armstrong_number(user_number):
       print(f"{user_number} is an Armstrong number.")
   else:
       print(f"{user_number} is not an Armstrong number.")

The program calls the is_armstrong_number function with the user’s input and prints a corresponding message based on whether the entered number is an Armstrong number or not. The message is displayed using an f-string to include the actual number.


Screenshot


Download

By Admin

Leave a Reply

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