GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing codescracker.com — Python Program to Generate Armstrong Numbers
Python Program to Generate Armstrong NumbersThis article is created to cover some programs in Python, that generates Armstrong numbers. Here are the list of programs:
Before creating these programs, let's remind about Armstrong numbers. What are Armstrong Numbers ?Any number that equals to sum of its own digits, where each digit raised to the power of number of digits. For example, 1634 is an Armstrong number. because: 14 + 64 + 34 + 44 = 1 + 1296 + 81 + 256 = 1297 + 337 = 1634 Note - Since 1634 is a 4-digit number, therefore each of its digit are raised to the power of 4 Generate and Print Armstrong Numbers from 1 to 1000To generate Armstrong numbers in python, you have to ask from user to enter the interval (starting and ending number), then generate and print Armstrong numbers between given range like shown in the program given below. The question is, write a Python program to generate Armstrong numbers from 1 to 1000. Here is its answer: print("Armstrong Numbers Between 1 and 1000:") first = 1 last = 1000 while first<=last: res = 0 temp = first noOfDigit = 0 while temp>0: temp = int(temp/10) noOfDigit = noOfDigit + 1 num = first while num>0: rem = num%10 pow = 1 i = 0 while i<noOfDigit: pow = pow*rem i = i+1 res = res+pow num = int(num/10) if res == first: print(res) first = first+1 The snapshot given below shows the sample output produced by this Python program: The dry run of above program goes like:
Generate Armstrong Numbers in Given RangeThis program receives interval (range) from user to generate and print Armstrong numbers between given range. print("Enter Range (Interval): ") first = int(input()) last = int(input()) if first>last: temp = first first = last last = temp while first<=last: res = 0 temp = first noOfDigit = 0 while temp>0: temp = int(temp/10) noOfDigit = noOfDigit + 1 num = first while num>0: rem = num%10 pow = 1 i = 0 while i<noOfDigit: pow = pow*rem i = i+1 res = res+pow num = int(num/10) if res == first: print(res) first = first+1 Here is its sample run: Now supply the input say 200 and 10000 as range or interval to find and print all Armstrong numbers between this range like shown in the snapshot given below: Modified Version of Previous ProgramThis is the modified version of previous program, uses try-except to handle with invalid inputs. The str() converts any type of value to a string type. The len() used in this program to find length of string (indirectly used to calculate number of digits): print("Enter Range (Interval): ", end="") try: first = int(input()) try: last = int(input()) if first>last: temp = first first = last last = temp print() while first<=last: res = 0 noOfDigit = len(str(first)) num = first while num>0: rem = num%10 res = res + (rem ** noOfDigit) num = int(num/10) if res == first: print(res, end=" ") first = first+1 print() except ValueError: print("\nInvalid Input!") except ValueError: print("\nInvalid Input!") Here is its sample run with range input as 100 and 5000: Same Program in Other Languages« Previous Program Next Program » Practice Quizzes
Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes. Popular Topics
Explore the most popular programming and computer science tutorials on CodesCracker. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |