Python Program to Find GCD of two Numbers

In this python tutorial, you will learn how to Find GCD of two Numbers using the while loop along with temp variable swap and print function of the python programming language.

Greatest Common Divisor or GCD is a mathematical term to find the greatest common factor that can perfectly divide the two numbers. A GCD is also known as the Highest Common Factor or HCF.

How to Find GCD of two Numbers?

Let’s take a look at the source code , here the values are given as input by the user in the code, the print function along with the while loop and the temp variable carry out the function.

RUN CODE SNIPPET
# Python Program to find GCD of Two Numbers a temp variable

num1 = int(input("Enter the first integer: "))
num2 = int(input("\nEnter the second integer: "))
a = num1
b = num2
while(num2 != 0):
# swap using temp variable
  temp = num2
  num2 = num1 % num2
  num1 = temp
gdc = num1
print("\nThe GDC of {0} and {1} is {2}".format(a,b,gdc))

INPUT:

25
75

OUTPUT:

Enter the first integer: 
Enter the second integer: 
The GDC of 25 and 75 is 25
  1. Here we give the user the option to enter the values and the input values are scanned using the input function and are stored in the variable num1 and num2 with the statements/strings ("Enter the first integer: ") and ("Enter the second integer: "),  we use the int function and declare the  input value as an integer value.
  2. The variables a and b are assigned, which will hold the values of num1 and num2
  3. We declare a while loop with the condition (num2 != 0), where the loop will iterate until the condition is met.
  4. We declare the variable temp which will hold the value of the variable num2 , the variable num2 is equal to the condition num1 % num2 where the num1 will hold the value of temp, thus forming a loop.
  5. Finally the gdc  will hold the value of num1, and the answer will be displayed with the statement ("\nThe GDC of {0} and {1} is {2}".format(a,b,gdc))  using the print function.
  6. In the above statement/string, the variables {0) ,{1) and {2) will hold the values a , b and gdc  where the format function helps in variable substitution and data formatting.

NOTE:

  • The != function is defined as not equal to operator, that is it returns True if operands on either side are not equal to each other, and returns False if they are equal.
  • The input() function allows a user to insert a value into a program
  • The While Loop is used to execute a block of statements repeatedly until a given condition is satisfied.
  • The Colon : operator is used to tell python that the next line of indented code should only be run if the condition is true.
  • The temp variable holds the default location for all temporary files, that is it is used in a situation, where we need to read multiple files, change or access the data in the file where it gives the output files based on the result of processed data.
  • The Modulo operator (%) calculates the remainder of dividing two values.
  • The variables used by the format function for substitution is enclosed in curly braces.
  • The print statement is followed by a period, to initiate the format function.
  • The print statement/string to be displayed in enclosed in double quotes.
  • The statement for the input function are enclosed in single quotes and parenthesis.
  • The \n in the code indicates a new line or the end of a statement line or a string.

Leave A Reply

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

You May Also Like

In this python tutorial, you will learn how to Display Prime Numbers Between Two Intervals using the if and else...
In this python tutorial, you will learn how to Calculate Standard Deviation with built in functions of the python programming...
In this Python program, we will convert temperature values from Celsius to Fahrenheit. The Celsius and Fahrenheit scales are two...