---
title: "Create a program to find the GCD (Greatest Common Divisor) of two numbers in Python."  
description: "Create a program to find the GCD (Greatest Common Divisor) of two numbers in Python."  
author: "Steilla Mitchel"  
published: 2023-06-18  
updated: 2023-06-19  
canonical: https://www.mindstick.com/forum/158764/create-a-program-to-find-the-gcd-greatest-common-divisor-of-two-numbers-in-python  
category: "python"  
tags: ["python", "programs"]  
reading_time: 2 minutes  

---

# Create a program to find the GCD (Greatest Common Divisor) of two numbers in Python.

Create a [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) to find the GCD ([Greatest](https://www.mindstick.com/articles/12976/the-greatest-games-at-vegas-paradise) [Common](https://www.mindstick.com/articles/23170/10-most-common-accounting-mistakes-of-small-business) Divisor) of two numbers.

## Replies

### Reply by Aryan Kumar

Sure, here is a Python program to find the GCD (Greatest Common Divisor) of two numbers in Python:

Python

```plaintext
def gcd(a, b):
  """
  Finds the greatest common divisor of two numbers.

  Args:
    a: The first number.
    b: The second number.

  Returns:
    The greatest common divisor of the two numbers.
  """

  if a < b:
    a, b = b, a

  while b != 0:
    a, b = b, a % b

  return a

if __name__ == "__main__":
  a = 10
  b = 20
  gcd_value = gcd(a, b)
  print(gcd_value)
```

This program works by first checking if the first number is less than the second number. If it is, then the program swaps the two numbers. Then, the program iterates through the numbers and keeps track of the remainder. The program breaks out of the loop when the remainder is 0. Finally, the program returns the first number.

To run the program, you can save it as a Python file and then run it from the command line. For example, if you save the program as `gcd.py`, you can run it by typing the following command into the command line:

Code snippet

```plaintext
python gcd.py
```

This will print the GCD of the two numbers to the console.

Here is an example of the output of the program:

Code snippet

```plaintext
$ python gcd.py
10
```

As you can see, the output of the program is 10, which is the GCD of 10 and 20.


---

Original Source: https://www.mindstick.com/forum/158764/create-a-program-to-find-the-gcd-greatest-common-divisor-of-two-numbers-in-python

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
