Python Program to Find the GCD of Two Numbers

Python Program to Find the GCD of Two Numbers

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

  1. Python Program to Add Two Numbers
  2. Python Program to Find/Calculate Sum of n Numbers
  3. Python Program to Find/Calculate Average of 3, 4, 5…n numbers
  4. Python Program to Print ASCII Value of Character
  5. Write a Program to Calculate Simple Interest in Python
  6. Python Program to Compute Compound Interest
  7. Leap Year Program in Python
  8. Python Program to Print Star Pattern
  9. Number Pattern Programs in Python
  10. Python Program to Print Even and Odd numbers From 1 to N
  11. Python Abs() Function: For Absolute Value
  12. How to Check Whether a Number is Fibonacci or Not in Python
  13. Python: Program to Find Power of Number
  14. Python Program to Reverse a Numbers
  15. Python Program to Print Prime Number 1 to N
  16. How to Find Square of Number in Python
  17. Python Program to Calculate Cube of Number
  18. Python Program to Find LCM of Two Numbers
  19. BMI (Body Mass Index) Calculator in Python
  20. Palindrome Program in Python using while loop, Function, etc
  21. Python Program to Swap Two Numbers
  22. Python Program to Get Standard Deviation
  23. Python Program to Find the Variance
  24. Python Program to Convert Height in cm to Feet and Inches
  25. Python Program to Convert Meters into Yards, Yards into Meters
  26. Python Program to Convert Kilometers to Meters, Miles
  27. Python Program to Find Perfect Number
  28. Python: Program to Find Strong Number
  29. Python Program Create Basic Calculator
  30. Python Program For math.floor() Method
  31. Python Program to Find Sum of Series 1/1! 2/2! 3/3! …1/n!
  32. Python Program to Check IF a Number is Power of Another Number
  33. Python Check Binary Representation of Given Number is Palindrome or Not
  34. Python Program to Draw a Pie Chart
  35. Python Program Input the Radius of Circle and Compute the Area
  36. Python Program to Calculate the Area of a Rectangle
  37. Python Program to Calculate Area of Triangle
  38. Python Program to Find Area and Circumference of Circle using Radius
  39. Python Program that Accepts Marks in 5 Subjects and Outputs Average Marks
  40. Python Program to Print Binary Value of Numbers From 1 to N

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *