Armstrong Numbers in Python

Armstrong Numbers in Python

Check if a given number is Armstrong using Python

For starters let us understand what Armstrong numbers are?

An Armstrong number is a positive number which when raised to the length of its digits is equal to the original number.

abcd... = a^n + b^n + c^n + d^n + ...
where n is the length of the number(abcd)

For example:

A number 407 has a length of 3

4^3 + 0^3 + 7^3 = 407

from the example above 407 is an Armstrong number. All good now!!

1694274_1.jpg

Source Code: Check Armstrong number of n digits

Let' s dive into the fun part, writing the code. First, Create a function call armstrong()

def armstrong():

Using the input() function allow the user to enter an integer as input which will be stored in the variable called number:

 number = int(input("Enter a number: "))

To check the length of a number in python, first convert the number into a string using the inbuilt str() function and store it in the variable n. Initialize the variable total_sum to 0 as we will be calculating the sum of the raised digits:

 n = len(str(number))
 total_sum=0

We will introduce a control structure that will help direct the order of execution of the statements in our program, the if ... else statement

All numbers that have one digit or have a length of one is an Armstrong number by default. To check this we use the if statement below and return a print statement if the condition is satisfied:

  if len(str(number)) == 1:
        print("All single digit numbers are Armstrong numbers")

We then check to ensure the user enters a positive number. The function then throws an exception when a negative number is entered and proceeds to call the function again so that the user can try again and enter a positive number :

  elif number < 0:
        print("Enter a positive number")
        armstrong()

In the Python language ecosystem, one can only loop through string data types, not integers, therefore, using the inbuilt str() function, we convert the number variable to a string so as to obtain the individual digits of the number.

First, loop through the number, assigning the digits to the variable x, secondly, raise the x to the initial variable n(as shown above), thirdly, add the raised digits and store them in the previously initialized variable total_sum

The code below makes use of nested if statements to print out whether the passed number is an Armstrong number or not.

else:
        for x in str(number):
            total_sum += int(x)**n
        if total_sum == number:
            print("This is an Armstrong number")
        else:
            print("This is not an Armstrong number")

Here is the whole source code:

def armstrong():
    number = int(input("Enter a number: "))
    n = len(str(number))
    total_sum=0

    if len(str(number)) == 1:
        print("All single digit numbers are Armstrong numbers")        
    elif number < 0:
        print("Enter a positive number")
        armstrong()
    else:
        for x in str(number):
            total_sum += int(x)**n
        if total_sum == number:
            print("This is an Armstrong number")
        else:
            print("This is not an Armstrong number")

armstrong()

Here is a snippet on how the program runs on the VS code terminal:

Capture.JPG

Thank you for reading, if you have any thoughts, get in touch in the comments section.

Here's to changing the society one code at a time. 🥂

Have a great day Pythonistas.