Friday, 27 December 2024

Day 61: Python Program to Find GCD of Two Numbers Using Recursion

 


def gcd(a, b):

    """

   Function to calculate GCD of two numbers using recursion

    """

    if b == 0:

        return a

    else:

        return gcd(b, a % b)

num1 = int(input("Enter the first number: "))

num2 = int(input("Enter the second number: "))

result = gcd(num1, num2)

print(f"The GCD of {num1} and {num2} is {result}")

#source code --> clcoding.com 

Code Explanation:

1. Function Definition:
def gcd(a, b):
    """
    Function to calculate GCD of two numbers using recursion
    """
def gcd(a, b):

This defines a function named gcd that takes two arguments, a and b.
a and b represent the two numbers for which we want to calculate the Greatest Common Divisor (GCD).
""" ... """

This is a docstring that describes what the function does. It mentions that the function calculates the GCD of two numbers using recursion.

2. Base Case:
    if b == 0:
        return a
if b == 0:

Checks whether b (the second number) is 0.
This is the base case of recursion. When b is 0, the recursion stops.
return a

If b is 0, the function returns the value of a, as the GCD of any number and 0 is the number itself.
Example: GCD(8, 0) → 8.

3. Recursive Case:
    else:
        return gcd(b, a % b)
else:

Executes if b is not 0.
return gcd(b, a % b)

This is the recursive call. The function calls itself with:
b as the new first argument.
a % b (the remainder when a is divided by b) as the new second argument.
This step reduces the size of the problem with each recursive call, eventually reaching the base case where b is 0.

4. Taking User Input:
num1 = int(input("Enter the first number: "))
num1 = int(input("Enter the first number: "))
Prompts the user to input the first number with the message "Enter the first number: ".
input() captures the user’s input as a string.
int() converts the string input to an integer so it can be used in calculations.
The integer value is stored in the variable num1.
num2 = int(input("Enter the second number: "))
num2 = int(input("Enter the second number: "))
Similar to the above, prompts the user to input the second number.
Converts the input string to an integer and stores it in the variable num2.

5. Calling the Function and Storing the Result:
result = gcd(num1, num2)
result = gcd(num1, num2)
Calls the gcd function with num1 and num2 as arguments.
The function computes the GCD of num1 and num2 and returns the result.
The returned GCD is stored in the variable result.

6. Printing the Result:
print(f"The GCD of {num1} and {num2} is {result}")
print(f"...")
Prints the GCD of the two numbers in a formatted message.
F-String (f"..."): Dynamically inserts the values of num1, num2, and result into the string.
Example Output: The GCD of 48 and 18 is 6.


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (96) AI (38) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (186) C (77) C# (12) C++ (83) Course (67) Coursera (246) Cybersecurity (25) Data Analysis (1) Data Analytics (2) data management (11) Data Science (138) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Euron (22) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Generative AI (6) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (75) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) Python (989) Python Coding Challenge (428) Python Quiz (66) Python Tips (3) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses

Python Coding for Kids ( Free Demo for Everyone)