Python – Program for checking a number is Armstrong or not?

Python – Program for checking a number is Armstrong or not?

In this post, We will be going to see different ways to write a code in Python for checking a number is Armstrong or not?

Armstrong number is a number in which the sum of each digit raised to the power of a number of digits present in a given number is equal to the number itself.

Example:

Input: Number = 153
Output: 153 is an Armstrong number
pow(1,3) + pow(5,3) + pow(3,3) 
1 + 125 + 27 
153
                                               
Input: Number = 121
Output: 121 is not a Armstrong number
pow(1,3) + pow(2,3) + pow(1,3) 
1 + 8 + 1
10

Now, Let’s see the Codes:

Code 1: Use string and convert data type concept.

# given number
number = 153

# find number of digits 
numOfDigits = len(str(number))

# initialise variable
summation = 0

# looping through each 
# digit of a number
for digits in str(number) :
    
    # each digit raised to the 
    # power of number of digits
    # and sum
    summation += int(digits) ** numOfDigits
    
if summation == number :
    print("Given number",number,
          "is an Armstrong number")
else:
    print("Given number",number,
          "is not a Armstrong number") 

Output:

Given number 153 is an Armstrong number

Code 2: Use string and convert data type concept along with lambda.

# given number
number = 153

# find number of digits 
numOfDigits = len(str(number))

# create a anonymous function
# for calculating digit raised  
# to the power of number of digits
reqPower = lambda digit, number : int(digit) ** len(str(number))

# initialise variable
summation = 0

# looping through each 
# digit of a number
for digits in str(number) :
    
    # each digit raised to the 
    # power of number of digits
    # and sum
    summation += reqPower(digits, number)
    
if summation == number :
    print("Given number",number,
          "is an Armstrong number")
else:
    print("Given number",number,
          "is not a Armstrong number")  

Output:

Given number 153 is an Armstrong number

Code 3: First find number of digits then calculate power and sum it.

# given number
number = 153

# take a temporary variable temp1
temp1 = number

# initialise variable
numOfDigits = 0

# calculate number of digits
if number == 0:
    numOfDigits = 1
    
else:
    while temp1 :
        temp1 //= 10
        numOfDigits += 1

# take a temporary variable temp2
temp2 = number

# initialise variable
summation = 0

# looping through each 
# digit of a number
while temp2 :
    
    # find remainder
    remainder = temp2 % 10
    
    # each digit raised to the power 
    # of number of digits and sum
    summation += (remainder ** numOfDigits)
    
    # update the variable
    temp2 //= 10
    
if summation == number :
    print("Given number",number, 
          "is an Armstrong number")
else:
    print("Given number",number,
          "is not a Armstrong number")   

Output:

Given number 153 is an Armstrong number

Code 4: By creating user defined function for calculating number of digits and required sum.

# Create a user-defined function
# for calculating count of digits
# in a number
def countDigits(number) :
    
    # take a temporary 
    # variable temp1
    temp1 = number

    # initialise variable
    numOfDigits = 0

    # calculate number of digits
    if number == 0:
        
        # return 1
        return 1

    else:
        while temp1 :
            temp1 //= 10
            numOfDigits += 1
        
        # return the count
        return numOfDigits

# Create a user-defined function
# for calculating sum of digit raised 
# to the power of a number of digits
def findSum(number) :
    
    # take a temporary 
    # variable temp2
    temp2 = number

    # initialise variable
    summation = 0

    numOfDigits = countDigits(number)
    
    # looping through each 
    # digit of a number
    while temp2 :

        # find remainder
        remainder = temp2 % 10

        # each digit raised to the power 
        # of number of digits and sum
        summation += (remainder ** numOfDigits)

        # update the variable
        temp2 //= 10
    
    # return the final value
    return summation

# main code
if __name__ == "__main__" :
    
    # given number
    number = 153

    # check for Armstrong number
    if number == findSum(number) :
        print("Given number",number,
              "is an Armstrong number")
    else:
        print("Given number",number,
              "is not a Armstrong number")   

Output:

Given number 153 is an Armstrong number

Leave a Reply

Your email address will not be published.

📢 Need further clarification or have any questions? Let's connect!

Connect 1:1 With Me: Schedule Call


If you have any doubts or would like to discuss anything related to this blog, feel free to reach out to me. I'm here to help! You can schedule a call by clicking on the above given link.
I'm looking forward to hearing from you and assisting you with any inquiries you may have. Your understanding and engagement are important to me!

This will close in 20 seconds