# Find common divisors of two numbers using Python

In this tutorial, we will **find the common divisors of two numbers using a Python program**. Common divisors are numbers that divide both the numbers perfectly. Here, we will learn what are common divisors, a method to find the common divisors and a Python program to find the common divisors of two numbers. If you want the implementation of the same using a Python program, you are in the right place.

So let’s continue reading this article to increase your knowledge…

## Method to find common divisors of two numbers

Common divisors are the numbers that perfectly divide both the given two numbers. To find the common divisors of two numbers, follow these steps-

- Store the two numbers in variable
*‘num1’*and*‘num2’*. - Declare a variable say
*‘i’*and initialize it with 1. - Check the divisibility of both numbers by
*‘i’*. - If both numbers are divisible, display the common divisor i.e.
*‘i’*. - Increment the value of
*‘i’*by 1. - Go to step 3 and repeat the steps until the value in
*‘i’*reaches the minimum among the two numbers i.e.*min(num1,num2)*. - Stop.

For example- Suppose the two numbers are 8 and 16.

num1 = 8 num2 = 16 Iteration-1 --> i=1 8 and 16 is divisible by i i.e. 1 Iteration-2 --> i=2 8 and 16 is divisible by i i.e. 2 Iteration-3 --> i=3 8 and 16 is not divisible by i i.e. 3 Iteration-4 --> i=4 8 and 16 is divisible by i i.e. 4 Iteration-5 --> i=5 8 and 16 is not divisible by i i.e. 5 Iteration-6 --> i=6 8 and 16 is not divisible by i i.e. 6 Iteration-7 --> i=7 8 and 16 is not divisible by i i.e. 7 Iteration-2 --> i=8 i.e. min(num1,num2) 8 and 16 is divisible by i i.e. 8 Common divisors --> {1, 2, 4, 8}

So, the common divisors of numbers 8 and 16 are 1, 2, 4, and 8.

## Python program to find all common divisors of two numbers

Now, we will see a Python program that finds the common divisors of two numbers. Firstly, we take the two numbers from the user as input. Using a *‘for’* loop, we check the divisibility of both numbers by loop control variable. The value of the loop control variable ranges from 1 to a minimum among the two numbers. If both the numbers are divisible, then we display the value of the loop control variable because it is a common divisor. The Python program is as follows-

num1 = int(input("ENTER FIRST NUMBER : ")) num2 = int(input("ENTER SECOND NUMBER : ")) divisor = 0 print("THE COMMON DIVISORS OF NUMBER ",num1," AND ",num2," ARE -") for i in range(1,min(num1,num2)+1): if num1%i == num2%i == 0: divisor = i print(divisor)

## Python program output

The above Python program displays all the common divisors of two numbers given by the user as input. The output after sample execution of the program is given below-

siddharth@siddharth-Lenovo-Y520-15IKBN:~/python$ python3 divisor.py ENTER FIRST NUMBER : 45 ENTER SECOND NUMBER : 90 THE COMMON DIVISORS OF NUMBER 45 AND 90 ARE - 1 3 5 9 15 45 siddharth@siddharth-Lenovo-Y520-15IKBN:~/python$

So, there are 6 common divisors of 45 and 90 – 1, 3, 5, 9, 15, and 45.

Thank you for reading this tutorial. I hope it helps you.

## Leave a Reply

You must be logged in to post a comment.