Data Science and Machine Learning Internship ...
- 0k Enrolled Learners
- Live Class
Python is easily one of the most popular and recognizable coding platforms out there. Thanks to its great features and versatility, everyone starting from amateurs all the way to pros use Python as their primary language of choice when it comes to programming. With that being said, one of the most common requests we get from patrons is how to write a program for Armstrong number in Python. Although this might seem very easy for someone who knows the in and out of the language, there are quite a few technicalities that one might miss out. Therefore in this article we will talk more about Armstrong number in Python and how you code a program for the same in Python.
Following pointers will be covered in this article,
Now that you know what an Armstrong number is, let us explore how you can write a program in Python for the same.
An Armstrong number in the most simple of terms can be defined as being an integer, of which the sum of the cubes of its digits is equal to the number itself. An example of an Armstrong number can be 371, which when computed can be broken down into 3**3 + 7**3 + 1**3 = 371.
Program for Armstrong number in Python
In order to write a program for Armstrong number in Python, you first need to have knowledge of the Python if…else Statement as well as Python while Loop.
Now that you know what the Python if…else Statement as well as Python while Loop does let us explore what a program in Python for Armstrong number will look like.
# Python program to check if the number provided by the user is an Armstrong number or not # take input from the user num = int(input("Enter a number: ")) # initialize sum sum = 0 # find the sum of the cube of each digit temp = num while temp > 0: digit = temp % 10 sum += digit ** 3 temp //= 10 # display the result if num == sum: print(num,"is an Armstrong number") else: print(num,"is not an Armstrong number")
In order to explore the above example better, let us take two inputs.
Input 1: 663 is entered when prompted.
Result: 663 is not an Armstrong number.
Input 2: 407 is entered when prompted.
Result: 407 is an Armstrong number.
In both the above inputs, we have the option of asking the user to enter a number of their choice and then analyze if it an Armstrong number or not.
In order to analyze if a certain input is an Armstrong number or not, we need to break the input into individual numbers, calculate the cube of each and then add them all together. To achieve this in the context of coding, we make use of the modulus operator (% operator). In the above example, remainder of a number when it is divided by 10 is the last digit of that number. We take the cubes using exponent operator.
In the final step, we compare our results to that of the original number entered and figure out if it an Armstrong number or not.
Program to check Armstrong number of n digits num = 1634 # Changed num variable to string, # and calculated the length (number of digits) order = len(str(num)) # initialize sum sum = 0 # find the sum of the cube of each digit temp = num while temp > 0: digit = temp % 10 sum += digit ** order temp //= 10 # display the result if num == sum: print(num,"is an Armstrong number") else: print(num,"is not an Armstrong number")
In the above program, we have already shared the input as being 1634. Therefore the program will now check if 1634 is an Armstrong number or not. As you might have guessed, the number 1634 is not an Armstrong number and therefore the above program prints, 1634 is not an Armstrong number.
This brings us to the end of this article on Armstrong Number In Python.
To get in-depth knowledge on Python along with its various applications, you can enroll here for live Python certification course online with 24/7 support and lifetime access. Got a question for us? Mention them in the comments section of this article and we will get back to you.
|Python Certification Training Course|
Class Starts on 28th May,2022
28th MaySAT&SUN (Weekend Batch)
|Python Certification Training Course|
Class Starts on 18th June,2022
18th JuneSAT&SUN (Weekend Batch)