Compute the greatest common divisor(GCD) and least common multiple( LCM)of two integers. LCM and GCD / HCF program in python. This python project is useful for beginners and CBSE KV School Class 11 and Class 12 students computer science practical file and NIELIT O Level Programming and Problem Solving through Python (Module M3-R5).
Objective- Compute the greatest common divisor and least common multiple of two integers using Python.
Highest Common Factor (HCF): The greatest common factor to any two or more than two integer numbers is known as HCF of those numbers. For example, HCF of 12 and 18 is 6. Also try:
Lowest Common Multiple (LCM): The smallest or lowest common multiple of any two or more than two integer numbers is termed as LCM. For example, LCM of 12 and 18 is 36.
Source Code :
Screenshot of the source code
Explaination of code :
def find_gcd(a,b): #function greatest common deviser taking a,b as input
gcd = 1 #Initialization
for i in range(1,a+1): #for loop
if a%i==0 and b%i==0: # checks for a divisor that divides both of a and b greater value will be come in gcd
gcd = i
first = int(input(‘Enter first number: ‘)) #entering input
second = int(input(‘Enter second number: ‘))
print(‘HCF or GCD of %d and %d is %d’ %(first, second, find_gcd(first, second)))
lcm = first * second / find_gcd(first, second)
print(‘LCM of %d and %d is %d’ %(first, second, lcm))
Download of Source Code – click here
Output of LCM and GCD program in python
Numbers are input 15 and 4. we know that HCF is 1 and LCM is 60.
Second number inputs are 20 and 15.we know HCF is 5 and LCM Is 60.
Both of the inputs are correct. Hence Testing is done.
The result is positive and objective is achieved.
for more python programs -> click here
Thanks for visit at Python Programs for Class 11 and 12 post.