You can mail me Contact Me!

Program to find the factorial of a number using Recursion

Python program to find the factorial of a number using Recursion

Algorithm (Step by Step):

  1. Start
  2. Define a recursive function factorial(n)
  3. Check if n is 0 or 1 (base case)
    • If true, return 1
    • If false, proceed to recursive case
  4. For recursive case, return n * factorial(n-1)
  5. Input a number from user
  6. Call factorial function with input number
  7. Display the result
  8. End

Hello, dear reader! 👋

Thanks for visiting my blog! I’m a student just like you, sharing what I learn to help others with Python programming. I hope my posts are useful for your studies! 😊

If you find this post helpful, please leave a comment—even just a few emojis will make my day! 🐍✨ Your feedback keeps me motivated to create more content for everyone. 🚀

Happy programming!
Abhin Krishna, S01, EB Department, MEC


Pseudocode:

FUNCTION factorial(n: INTEGER) -> INTEGER
    IF n = 0 OR n = 1 THEN
        RETURN 1
    ELSE
        RETURN n * factorial(n - 1)
    END IF
END FUNCTION

ALGORITHM FindFactorial
    BEGIN
        INPUT number
        result ← factorial(number)
        OUTPUT "The factorial of ", number, " is ", result
    END
Program:

def factorial(n):
    # Base case: factorial of 0 or 1 is 1
    if n == 0 or n == 1:
        return 1
    else:
        # Recursive case: n * factorial of (n - 1)
        return n * factorial(n - 1)

def main():
    try:
        # Input the number
        number = int(input("Enter a number: "))
        
        # Check for negative numbers
        if number < 0:
            print("Factorial cannot be calculated for negative numbers.")
            return
            
        # Calculate factorial
        result = factorial(number)
        
        # Print the result
        print(f"The factorial of {number} is {result}")
        
    except ValueError:
        print("Please enter a valid integer.")

# Call main function
if __name__ == "__main__":
    main() 
Flowchart:
flowchart TD A([Start]) --> B[/Enter a number/] B --> C{Is input valid?} C -->|No| D[/Display: Please enter a valid integer/] --> E([End]) C -->|Yes| F{Is number negative?} F -->|Yes| G[/Display: Factorial cannot be calculated for negative numbers/] --> E F -->|No| H[Call factorial n] H --> I{Is n = 0 or n = 1?} I -->|Yes| J[Return 1] I -->|No| K[Compute n * factorial n-1] K --> I J --> L[Calculate final result] L --> M[/Display: The factorial of n is result/] M --> E %% Styling style A fill:#f9f,stroke:#333,stroke-width:2px style E fill:#f9f,stroke:#333,stroke-width:2px style B fill:#90caf9,stroke:#333,stroke-width:2px style D fill:#90caf9,stroke:#333,stroke-width:2px style G fill:#90caf9,stroke:#333,stroke-width:2px style M fill:#90caf9,stroke:#333,stroke-width:2px style C fill:#ffd700,stroke:#333,stroke-width:2px style F fill:#ffd700,stroke:#333,stroke-width:2px style I fill:#ffd700,stroke:#333,stroke-width:2px style H fill:#a5d6a7,stroke:#333,stroke-width:2px style J fill:#a5d6a7,stroke:#333,stroke-width:2px style K fill:#a5d6a7,stroke:#333,stroke-width:2px style L fill:#a5d6a7,stroke:#333,stroke-width:2px

Important!
If you find any mistakes in my code or flowchart, please comment below this post. I will be happy to correct them and clear up any doubts you may have.


Flowchart Image:


Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
Site is Blocked
Sorry! This site is not available in your country.