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
return gcd
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
Testing
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.
Result
The result is positive and objective is achieved.
for more python programs -> click here
Input a welcome message and display it in Python
Display the larger / smaller number in Python.
Greatest of Three Numbers in Python using Nested if
Patterns using nested loop in Python
Program to Print Pattern in Python
Program to input the value of x and n and sum of series in Python
Python Program for Armstrong, Prefect Number, Palindrome
Program of Prime number by recursion in python
Prime Number Program in Python
Write a Program to Print Fibonacci Series in Python
Python program to count number of vowels in a string
Whether a String is Palindrome or not in Python
Linear search in python using list
Program to read a text file in python
Python program to read a file line by line
Program to Count Vowels and Consonants in Python
Python Leap Year Program for Beginners
Python Program to Print Series and Addition
Binary Search Program in Python
Program to find sum of digits in python
Sum of numbers divisible by 3 and 5 in python
Thanks for visit at Python Programs for Class 11 and 12 post.