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
- 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
andnum2
with the statements/strings("Enter the first integer: ")
 and("Enter the second integer: ")
, Â we use theint
function and declare the input value as an integer value. - The variables
a
andb
are assigned, which will hold the values ofnum1
andnum2
- We declare a
while
loop with the condition(num2 != 0)
, where the loop will iterate until the condition is met. - We declare the variable
temp
which will hold the value of the variablenum2
, the variablenum2
is equal to the conditionnum1 % num2
where thenum1
will hold the value oftemp
, thus forming a loop. - Finally the
gdc
 will hold the value ofnum1
, and the answer will be displayed with the statement("\nThe GDC of {0} and {1} is {2}".format(a,b,gdc))
 using theprint
function. - In the above statement/string, the variables
{0)
,{1)
and{2)
will hold the valuesÂa
,Âb
andgdc
 where theformat
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.