Python program to find the gcd of two numbers; Through this tutorial, you will learn how to find the greatest common divisor (GCD) of the two numbers or array elements in the Python program using for loop, function.
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
- Python program to find the GCD of two non-zero numbers
- Python program to find the GCD of the array
Python program to find the GCD of two non-zero numbers
- Allow user to input numbers.
- Include math module.
- Calculate 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.
Now we will find the GCD of an array element or more than two non-zero numbers. So, let’s go to write a Python program by simply using the above-given python program concepts.
Python program to find the GCD of the array
#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.
Recommended Python Programs
- Python Program to Add Two Numbers
- Python Program to Find/Calculate Sum of n Numbers
- Python Program to Find/Calculate Average of 3, 4, 5…n numbers
- Python Program to Print ASCII Value of Character
- Write a Program to Calculate Simple Interest in Python
- Python Program to Compute Compound Interest
- Leap Year Program in Python
- Python Program to Print Star Pattern
- Number Pattern Programs in Python
- Python Program to Print Even and Odd numbers From 1 to N
- Python Abs() Function: For Absolute Value
- How to Check Whether a Number is Fibonacci or Not in Python
- Python: Program to Find Power of Number
- Python Program to Reverse a Numbers
- Python Program to Print Prime Number 1 to N
- How to Find Square of Number in Python
- Python Program to Calculate Cube of Number
- Python Program to Find LCM of Two Numbers
- BMI (Body Mass Index) Calculator in Python
- Palindrome Program in Python using while loop, Function, etc
- Python Program to Swap Two Numbers
- Python Program to Get Standard Deviation
- Python Program to Find the Variance
- Python Program to Convert Height in cm to Feet and Inches
- Python Program to Convert Meters into Yards, Yards into Meters
- Python Program to Convert Kilometers to Meters, Miles
- Python Program to Find Perfect Number
- Python: Program to Find Strong Number
- Python Program Create Basic Calculator
- Python Program For math.floor() Method
- Python Program to Find Sum of Series 1/1! 2/2! 3/3! …1/n!
- Python Program to Check IF a Number is Power of Another Number
- Python Check Binary Representation of Given Number is Palindrome or Not
- Python Program to Draw a Pie Chart
- Python Program Input the Radius of Circle and Compute the Area
- Python Program to Calculate the Area of a Rectangle
- Python Program to Calculate Area of Triangle
- Python Program to Find Area and Circumference of Circle using Radius
- Python Program that Accepts Marks in 5 Subjects and Outputs Average Marks
- Python Program to Print Binary Value of Numbers From 1 to N