Homework 5 Problem 1
Blade
python
a year ago
1.0 kB
28
Indexable
"""
1. Write a Python function called is_armstrong(number) that takes a three-digit
number as its parameter and returns True or False based on the nature of the
number. If the function returns true, print {number} is an Armstrong number.
Otherwise print {number} is not an Armstrong number. (10 pts).
Example
is_armstrong(153) ---> return True
is_armstrong(121) ---> return True
"""
# Write your solution for problem 1 here.
import math
def is_armstrong(number): # "153"
# how to take the number and seperate it into 3 digits
if number < 100 or number > 999:
return False
string_number = str(number)
firstDigit = int(string_number[0])
secondDigit = int(string_number[1])
lastDigit = int(string_number[2])
result = pow(firstDigit, 3) + pow(secondDigit, 3) + pow(lastDigit, 3)
if result == number:
print(f"{number} is an Armstrong number.")
return True
else:
print(f"{number} is not Armstrong number.")
return FalseEditor is loading...
Leave a Comment