Find the GCD of two numbers
AIM:
To write a program to find the GCD of two numbers using function.
Equipments Required:
- Hardware โ PCs
- Anaconda โ Python 3.7 Installation / Moodle-Code Runner
Algorithm
- Define a function.
- Get the two numbers from the user.
- Compare the two values, to find the smaller number.
- Use for() and if() loop to find the GCD of the two numbers.
Program:
/*
Program to find the gcd of two number using function.
Developed by: Y Shavedha
RegisterNumber: 21500429
*/
def gcd()
n1=int(input())
n2=int(input())
if (n1>n2):
smaller=n2
else:
smaller=n1
for i in range(1,smaller+1):
if (n1%i==0 and n2%i==0):
gcd=i
print("GCD of two umbers is:",gcd)
Output:
Result:
Thus the program to find the GCD of two numbers is written and verified using python programming.