Programming Examples

Write a Program to calculate factorial of a number using recursion.


Write a Program to calculate factorial of a number using recursion.

Solution

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("Factorial of", num, "is:", factorial(num))
▶ RUN

Output/ Explanation:

Enter a number: 5

Factorial of 5 is: 120

Latest Current Affairs 2026 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online MBA 2 years Online MCA Online BCA Best Website and Software Company in Allahabad