Exercise 65
Write a function in Python which takes as argument a tuple (a, b) composed of two integers and returns it the greatest common divisor GCD of a and b without using any predefined function in python.
Solution
def greatestCommonDiv(a,b):
# take d = a as initial value
d = a
# while d is not common divisor
while (a%d != 0 or b%d !=0):
# we decremente d
d = d - 1
return d
# testing function
print("The geatest common divisor of 8 and 12 is d = ",greatestCommonDiv(8,12))
# The output is : The geatest common divisor of 8 and 12 is d = 4
Younes Derfoufi
my-courses.net
my-courses.net
Thank you for sharing an amazing & wonderful blog. This content is very useful, informative and valuable in order to enhance knowledge. Keep sharing this type of content with us & keep updating us with new blogs. Apart from this, if anyone who wants to join the Python training institute in Delhi, can contact 9311002620 or visit our website-
https://www.htsindia.com/Courses/python/python-training-institute-in-south-delhi