Python program to find the GCD of the array

Here, we will figure out how to locate the best normal divisor (GCD) of the exhibit components in the Python programming language?

GCD of at least two non-zero number is the biggest number that partitions both or more non-zero numbers. It is one of the fundamental ideas of science.

Example:

    Input : 
    8, 4

    Output : 
    4 

    Explanation:
    8 and 4 are two non-zero numbers which are divided by 2 and 
    also by 4 but 4 is the largest number than 2. So, the GCD of 8 and 4 is 4. 

Here, a variety of the non-zero numbers will be given by the client and we need to discover the GCD of the cluster components in Python.

To tackle this issue, we will utilize the math module of Python. It is one of the most valuable modules of Python utilized for scientific activities.

In this way, before going to understand this we will figure out how to discover the GCD of two non-zero numbers.

Python program to discover the GCD of two non-zero numbers:

# importing the module
import math

# input two numbers
m,n=map(int,input('Enter two non-zero numbers: ').split())

#to find GCD
g=math.gcd(m,n) 

# printing the result
print('GCD of {} and {} is {}.'.format(m,n,g))

Output:

Run 1: 
Enter two non-zero numbers: 8 4
GCD of 8 and 4 is 4.

Run 2:
Enter two non-zero numbers: 28 35
GCD of 28 and 35 is 7.

Presently, we have figured out how to discover the GCD of two non-zero number yet our

primary assignment is to discover the GCD of an exhibit component or more than two non-zero numbers.

Along these lines, we should go to compose a Python program by basically utilizing the above ideas.

Python program to discover the GCD of the array:

# importing the module
import math

# array of integers
A=[40,15,25,50,70,10,95]

#initialize variable b as first element of A
b=A[0]  
for j in range(1,len(A)):
    s=math.gcd(b,A[j])
    b=s
print('GCD of array elements is  {}.'.format(b))

Output:

GCD of array elements is 5. 

Leave a Comment

error: Alert: Content is protected!!