You are on page 1of 1

Ex: No: 1 Date:

GCD OF TWO NUMBERS Aim: To write a

Python program to find GCD of two numbers Algorithm:

1. Read two numbers from users x and y


2. Find the smallest among the two inputs x and y
3. Repeat the following steps until i reaches smaller+1, then goto step 4
3.1 Check if x and y are divisible by i.
3.2 If it is True then, Assign gcd=i
4. Print the value of gcd

Program:

x = int(input("Enter first number: "))


y = int(input("Enter second number: "))
if x < y:
smaller = x else:
smaller = y
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)): gcd = i
print "The GCD. of", x,"and", y,"is",gcd

Sample output:

/usr/bin/python3.4 /home/student/Desktop/fdrive/Exp1.py Enter first number: 24

Enter second number: 15

The GCD. of 24 and 15 is 3

Conclusion:

Thus the Python program to compute GCD of two numbers has been executed
successfully.

You might also like