Prime

#### Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

#### Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

# Program for GCD of two numbers in Python

## GCD of Two numbers :

GCD of two or more integers (where integers not equal to zero) is largest positive integer that divides each of the integers. For example, GCD of  12 and 16 is 4 , where factors of 12==>1,2,4,6,12
factors of 16==>1,2,4,8,16
common factors are 1,2,4
greatest common factor is 4.

## Working :

• Step 1: import math module
• Step 2: read the first element
• Step 3:read the second element
• Step 4:use in-built gcd function to print gcd of two numbers

## Python code:

### Solution 1:

import math
n1=int(input(“ENTER THE FIRST NUMBER “))
n2=int(input(“ENTER SECOND NUMBER “))
print(“THE GCD OF GIVEN NUMBERS: “,math.gcd(n1,n2))
`ENTER THE FIRST NUMBER 12ENTER SECOND NUMBER 16THE GCD OF GIVEN NUMBERS: 4`

## Working :

• Step 1: Create a function gcd which takes  two arguments
• Step 2: This function works on euclidean algorithm
• Step 3: Read first number
• Step 4: Read second number
• Step 5: Call the function by passing our inputs as arguments and print the result

### Solution 2:

def gcd(n1,n2):
if(n1==0):
return n2
else:
return gcd(n2%n1,n1)
n1=int(input(“ENTER THE FIRST NUMBER “))
n2=int(input(“ENTER SECOND NUMBER “))
print(“GCD of given two numbers is:”,gcd(n1,n2))
`ENTER THE FIRST NUMBER 64ENTER SECOND NUMBER 24GCD of given two numbers is: 8`