B.Sc Computer Science with Data Analytics
Python Programming - Lab
Core Lab 5
Bharathiar University
Program 6 - Write recursive functions for the factorial of positive integer.
SOURCE CODE:
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
# Read an integer
num = int(input("Enter a positive integer: "))
# Check if the input is a positive integer
if num < 0:
print("Factorial is not defined for negative numbers.")
else:
result = factorial(num)
print("Factorial of", num, "is:", result)
OUTPUT:
Enter a positive integer: 5
Factorial of 5 is: 120
No comments:
Post a Comment