Wednesday, 11 December 2024

Day 31 : Python Program to Find Fibonacci Numbers using Recursion

 


def factorial(n):

    if n == 0 or n == 1:

        return 1

    else:

    return n * factorial(n - 1)

num = int(input("Enter a number: "))

if num < 0:

    print("Factorial is not defined for negative numbers.")

else:

    print(f"The factorial of {num} is {factorial(num)}.")


Code Explanation:

1. Factorial Function Definition:

def factorial(n):
    if n == 0 or n == 1:
        return 1
    else:
        return n * factorial(n - 1)
The function factorial(n) calculates the factorial of a number n recursively.
The base case for the recursion is when n is 0 or 1:
factorial(0) = 1
factorial(1) = 1
This is because the factorial of both 0 and 1 is defined as 1.
For any other value of n greater than 1, the function returns n * factorial(n - 1). This is the recursive call that keeps reducing n by 1 until it reaches 1.

For example:
factorial(5) will calculate 5 * factorial(4)
factorial(4) will calculate 4 * factorial(3)
factorial(3) will calculate 3 * factorial(2)
factorial(2) will calculate 2 * factorial(1)
Once factorial(1) is reached, it returns 1, and all the previous recursive calls return their results back, giving us the final answer.

2. Taking User Input:

num = int(input("Enter a number: "))
This line takes an integer input from the user and stores it in the variable num.
input() reads input as a string, so int() is used to convert it into an integer.

3. Checking for Negative Input:

if num < 0:
    print("Factorial is not defined for negative numbers.")
else:
    print(f"The factorial of {num} is {factorial(num)}.")
This block checks if the input number is negative.
If the number is negative, it prints: "Factorial is not defined for negative numbers." because the factorial is only defined for non-negative integers (0, 1, 2, 3, ...).
If the number is not negative, it calls the factorial() function and prints the result using print().

Example Execution:
Input:
Enter a number: 5

Step-by-Step Explanation:
The user inputs 5 which is stored in num.
The if condition checks if the number is negative (num < 0). Since 5 is not negative, it proceeds to the else block.
The factorial(5) function is called:
factorial(5) → 5 * factorial(4)
factorial(4) → 4 * factorial(3)
factorial(3) → 3 * factorial(2)
factorial(2) → 2 * factorial(1)
factorial(1) returns 1

Now, all the recursive calls return their results:
factorial(2) = 2 * 1 = 2
factorial(3) = 3 * 2 = 6
factorial(4) = 4 * 6 = 24
factorial(5) = 5 * 24 = 120

The final result 120 is printed: "The factorial of 5 is 120."

#source code --> clcoding.com 

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (41) AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (225) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (925) Python Coding Challenge (343) Python Quiz (12) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

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