Python Programming for Armstrong Number

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. For example, 153 is an Armstrong number because 13+53+33=1531^3 + 5^3 + 3^3 = 153

Here's a simple Python program to check if a number is an Armstrong number:

 def is_armstrong(number):  
   # Convert number to string to easily iterate over digits  
   digits = str(number)  
   num_digits = len(digits)  
   # Calculate the sum of digits raised to the power of the number of digits  
   sum_of_powers = sum(int(digit) ** num_digits for digit in digits)  
   return sum_of_powers == number  
 # Get user input  
 try:  
   user_input = int(input("Enter a number: "))  
   if is_armstrong(user_input):  
     print(f"{user_input} is an Armstrong number.")  
   else:  
     print(f"{user_input} is not an Armstrong number.")  
 except ValueError:  
   print("Please enter a valid integer.")  

How It Works:

  1. The function is_armstrong takes a number as input and converts it to a string to easily iterate through each digit.
  2. It calculates the number of digits and then computes the sum of each digit raised to the power of the total number of digits.
  3. Finally, it compares the calculated sum to the original number to check if it’s an Armstrong number.

Running the Program:

  1. Copy the code into a Python environment (like IDLE, Jupyter Notebook, or any online Python compiler).
  2. Run the program and enter a number when prompted. It will tell you whether the number is an Armstrong number or not.

Thanks

Kailash Chandra Behera

An IT professional with over 13 years of experience in the full software development life cycle for Windows, services, and web-based applications using Microsoft .NET technologies. Demonstrated expertise in delivering all phases of project development—from initiation to closure—while aligning with business objectives to drive process improvements, competitive advantage, and measurable bottom-line gains. Proven ability to work independently and manage multiple projects successfully. Committed to the efficient and effective development of projects in fast-paced, deadline-driven environments. Skills: Proficient in designing and developing applications using various Microsoft technologies. Total IT Experience: 13+ years

Previous Post Next Post

نموذج الاتصال