Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 10 - Lists - Programming Exercises - Page 351: 10.12

Answer

code

Work Step by Step

# 10.12 (Compute GCD) Write a function that returns the greatest common divisor # (GCD) of integers in a list. Use the following function header: # def gcd(numbers): # Write a test program that prompts the user to enter five numbers, invokes the # function to find the GCD of these numbers, and displays the GCD. def gcd(numbers): gcd = min(numbers) i = 0 while True: if numbers[i] % gcd != 0: gcd-=1 i = 0 else: i+=1 if i == len(numbers): break return gcd def main(): lst = input("Enter numbers: ").split() lst = [int(x) for x in lst] print(gcd(lst)) main()
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.