Write a python program to check Armstrong number using object oriented approach


This Python program defines a class called ArmstrongChecker that checks whether a given number is an Armstrong number. Here's a step-by-step explanation of how it works:

  • The ArmstrongChecker class is defined with a constructor __init__, which takes one argument, num, representing the number to be checked.
  • Inside the constructor, self.num is assigned the value of the num argument, which stores the number to be checked for being an Armstrong number.
  • The class has a method called is_armstrong(). This method checks if the number stored in self.num is an Armstrong number and returns a Boolean value (True or False).
  • Inside the is_armstrong method: a. num_str is calculated by converting the number to a string. This is done to count the number of digits in the original number.
    • num_digits is calculated as the length of num_str, which gives the number of digits in the original number.
    • digit_sum is calculated using a generator expression. It calculates the sum of each digit raised to the power of num_digits. For example, if num is 153, digit_sum would be calculated as (1^3 + 5^3 + 3^3), which is 153.
    • Finally, the method returns True if digit_sum is equal to the original number self.num, indicating that the number is an Armstrong number. Otherwise, it returns False.
  • The program then takes user input to enter a number using int(input("Enter a Number : ")) and stores it in the variable num.
  • An instance of the ArmstrongChecker class is created with the provided number num as an argument, and the instance is stored in the variable checker.
  • The program then checks if the number is an Armstrong number by calling the is_armstrong() method of the checker object.
  • If the number is an Armstrong number (i.e., the method returns True), it prints a message indicating that the number is an Armstrong number. Otherwise, it prints a message indicating that the number is not an Armstrong number.

The program checks whether a given number is an Armstrong number using the Armstrong number definition (a number is an Armstrong number if the sum of its digits raised to the power of the number of digits is equal to the original number). It does so by encapsulating the logic in a class called ArmstrongChecker.


Source Code

class ArmstrongChecker:
    def __init__(self, num):
        self.num = num
 
    def is_armstrong(self):        
        num_str = str(self.num)	# Convert the number to a string to count the digits        
        num_digits = len(num_str)	# Calculate the number of digits
        digit_sum = sum(int(digit) ** num_digits for digit in num_str)	# Calculate the sum of the nth powers of its digits        
        return digit_sum == self.num	# Check if the sum is equal to the original number
 
num = int(input("Enter a Number : "))
 
checker = ArmstrongChecker(num)
 
if checker.is_armstrong():
    print(f"{num} is an Armstrong Number")
else:
    print(f"{num} is not an Armstrong Number")

Output

Enter a Number : 371
371 is an Armstrong Number

Example Programs