codescracker


python

Python Program to Generate Armstrong Numbers



« Previous Program Next Program »


Generate Armstrong Numbers in Python

To generate armstrong numbers in python, you have to ask from user to enter the interval or simply enter starting and ending number to generate armstrong numbers in between the interval given by the user as shown in the program given below.

Python Programming Code to Generate Armstrong Numbers

Following python program ask from user to enter interval to generate and print armstrong numbers between the interval entered by the user:

# Python Program - Generate Armstrong Numbers
		
while True:
	print("Enter 'x' for exit.")
	print("Enter the interval (starting and ending number): ")
	start = input()
	end = input()
	if start == 'x':
		break
	else:
		lower = int(start)
		upper = int(end)
		for num in range(lower, upper+1):
			tot = 0
			temp = num
			while temp != 0:
				dig = temp % 10
				tot += dig ** 3
				temp //= 10
			if num == tot:
				print(num)
		print("\n")

Here is the sample run of the above python program to illustrate how to generate armstrong numbers:

python generate armstrong numbers

Same Program in Other Languages

You may also like to learn or practice the same program in other popular programming languages:


« Previous Program Next Program »




Tools
Calculator

Quick Links
Signup - Login - Give Online Test