Codementor Events

How to Print a Series of Armstrong Numbers Using Python

Published Jun 02, 2022Last updated Nov 28, 2022
How to Print a Series of Armstrong Numbers Using Python

Introduction

Python programming language is equipped with versatility and ease. It is being used to do multiple programming tasks, from the very simple to the very complex. Learning python programming is not just a great way to begin learning to program but also a brilliant way to boost your career prospects.

In this blog, let us take a look at a basic programming task using python programming. This is one of those tasks that serve as a foundational step for beginners. The is to print a series of Armstrong Numbers in Python.

What is an Armstrong Number in Python?

An Armstrong number in python, also known as a narcissistic number, follows each of the given set of conditions:

  • Each of its digits is raised to the power of the number of digits in it.
  • Then, the results derived in step 1 for each digit are added.
  • The sum derived in step 2 must be equal to the number itself.

An example of an Armstrong number in Python is 153.

It follows the above-stated set of conditions as shown:

  • 1³, 5³, and 3³ are calculated.
  • 13 + 53 + 33 is calculated.
  • The sum derived in the previous step is 153, which is equal to the number 153 itself.

There are other such numbers that may be said to be Armstrong numbers in Python. Some of them are 371, 1634, 9474, 93084, and so on.

How to Check if a Number is an Armstrong Number in Python?

In order to decide whether a number is an Armstrong number in python or not, we can use the aforementioned steps. For that, we would need to know the length of the number or the total number of digits in it.

Then, first, we would need to calculate the values of each digit raised to the power of the number of digits. Second, we would need to find the sum of all such values. Finally, we would need to compare the derived sum with the original number.

If the number is an Armstrong number, the value of both will be the same. If this does not hold true, then the number is not an Armstrong number.

Following is the code snippet for the same:

def is_Armstrong(num):
 size = len(str(num))
 hold = num
 sum = 0
 while(hold>0):
  digit = hold % 10
  sum += digit**size
  hold //= 10
 if(sum==num):
  print("Armstrong Number")
 else:
  print("Not Armstrong Number")
is_Armstrong(153)
is_Armstrong(1)
is_Armstrong(99)

Output (for inputs as 153, 1, 99 respectively):

Armstrong Number
Armstrong Number
Not Armstrong Number

Printing a Series of Armstrong Numbers Using Python

This might be done in two ways:

  • Printing a series of first 'N' Armstrong numbers
  • Printing a series of Armstrong numbers in a given range

Let us now understand how to go about implementing each of the approaches.

1. Printing the First "N" Armstrong Numbers Using Python

To do this, we would have to start checking from the first Armstrong number, that is, 1, and go till we find the Nth Armstrong number. We can do this simply by using a loop that runs from 1 to the Nth Armstrong number. We can use a "count" variable and initialize it to zero to keep track of how many Armstrong numbers have been found.

The loop would be executed only when the value of the count is less than "N" or till the time the Nth Armstrong number is reached. At each iteration, when a number satisfies the condition of being an Armstrong number, we would print it and increment the count by 1. Then, increment the value of the number by 1 to select the next number.

Following is the code snippet for the same:

def first_N_Armstrong(N):
 count = 0
 num = 1
 while(count<N):
  size = len(str(num))
  hold = num
  sum = 0
  while(hold>0):
   digit = hold % 10
   sum += digit**size
   hold //= 10
  if(sum==num):
   print(num)
   count = count + 1
  num = num + 1
first_N_Armstrong(10)

Output (for N=10):

1
2
3
4
5
6
7
8
9
153

2. Printing a Series of Armstrong Numbers in a Pre-defined Range Using Python

To do this, we would need a range, that is, a lower limit and an upper limit, between which we would be checking which of the numbers is an Armstrong number. So, in this case, instead of starting the loop with "1" as the first valid Armstrong number, we would simply start with the number, which is defined as the lower limit of the given range. We would run the loop until we reach the range's upper limit.

At each iteration, we would check if the selected number is Armstrong or not. If it satisfies the condition mentioned above, we would print it; otherwise, simply continue to the next iteration by selecting the next number in the range.

Following is the code snippet for the same:

def range_Armstrong(llimit, ulimit):
 for num in range (llimit, ulimit):
  size = len(str(num))
  hold = num
  sum = 0
  while(hold>0):
   digit = hold % 10
   sum += digit**size
   hold //= 10
  if(sum==num):
   print(num)
  num = num + 1
range_Armstrong(10, 500)

Output (for range = {10, 500}):

153
370
371
407

Conclusion

The task to print a series of Armstrong numbers in Python can be completed with ease if we break it down into smaller steps. We can do this in two ways, either by printing the first "N" Armstrong numbers or by printing all the Armstrong numbers that exist in a predefined range. For each number, we would be checking the condition for Armstrong number, that is if the sum of the values of each digit raised to the power of the number of digits equals the number itself.

Armstrong numbers are special numbers that find applications in number theory and cryptography.

References:

  1. https://www.scaler.com/topics/armstrong-number-in-python/

Important Python Topics you should read:

  1. Matrix Multiplication in Python by Scaler
  2. find() function in Python by Scaler

Happy Learning!

Discover and read more posts from Sandeep M
get started
post comments5Replies
kbsab
a year ago

Printing a series of Armstrong Numbers using Python is like advancing through the coding curriculum, much like progressing through the Japanese school year. Each number echoes the growth and mastery of coding skills, a journey akin to the academic steps in a school year.
https://www.ejable.com/japan-corner/education-in-japan/japanese-educational-system/

Alban Kodra
a year ago

Connecting this insightful explanation to gaming, Python’s versatility can be harnessed to enhance gaming experiences. Imagine creating a game where certain in-game elements, like character abilities or score calculations, follow the logic of Armstrong Numbers. This could add a unique and challenging aspect to gameplay, requiring players to strategize and engage in problem-solving.

Additionally, understanding the process of checking for Armstrong Numbers in Python might inspire game developers to implement similar algorithms for in-game tasks or puzzles. Gamification of programming concepts can not only make learning more engaging but also introduce players to computational thinking.

Incorporating Python into game development opens up opportunities for creative and innovative features, making games like https://appvalleyz.com/tr/indir-undertale-apk/ not only entertaining but also intellectually stimulating. This blog serves as a reminder that Python’s power extends beyond traditional programming tasks and can be a valuable tool for crafting dynamic and engaging gaming experiences."

elmen karm
2 years ago

The post it’s so great but i adding here that how you can print a series of Armstrong numbers using Python in a simple way:
lower = 100
upper = 1000

for num in range(lower, upper + 1):
order = len(str(num))
sum = 0

temp = num
while temp > 0:
digit = temp % 10
sum += digit ** order
temp //= 10

if num == sum:
print(num)
This code will print all Armstrong numbers between 100 and 1000 (inclusive). You can modify the lower and upper variables to print Armstrong numbers in a different range.

As for adding a link as an example, I’m not sure how it’s related to the code you’ve requested, but here’s an example of how you can add a link using Python:
link = https://carparkingmultiplayermodapk.net/
print("Example link: " + link)
This code will print the message "Example link: to the console.

Show more replies