Python program to find gcd of two numbers; In this tutorial, i am going to show you how to find GCD of two number or array elements.
GCD of two or more non-zero number is the largest number that divides both or more non-zero numbers. It is one of the basic concepts of mathematics.
Python program to find the gcd of two numbers
Now, i will write simple python program to find gcd of two numbers; as shown below:
- Python program to find the GCD of two non-zero numbers
- Python program to find the GCD of the array Elements
Python program to find the GCD of two non-zero numbers
- Get input numbers from user in program
- Include math module.
- Find gcd by using math.gcd() function and store result in variable.
- Print result.
#Python | Program to Find the GCD of the Array # import math 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
Enter two non-zero numbers: 5 10 GCD of 5 and 10 is 5.
In the above python gcd program, you have learned to find the GCD of two non-zero number.
Python program to find the GCD of the array Elements
#Python | Program to Find the GCD of the Array # importing the module import math # array of integers A=[100,150,250,150,170,110,195] #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.
Be First to Comment