Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
127 views

Python Program For Factorial of A Number

This Python program defines a recursive function called factorial that takes a number as a parameter and returns its factorial. It uses a ternary operator to return 1 for 0 or 1, and otherwise multiplies the number by the factorial of one less than the number. The code then calls the factorial function on the number 5 and prints the result.

Uploaded by

Lakshmy
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
127 views

Python Program For Factorial of A Number

This Python program defines a recursive function called factorial that takes a number as a parameter and returns its factorial. It uses a ternary operator to return 1 for 0 or 1, and otherwise multiplies the number by the factorial of one less than the number. The code then calls the factorial function on the number 5 and prints the result.

Uploaded by

Lakshmy
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

# Python 3 program to find

# factorial of given number


def factorial(n):

# single line to find factorial


return 1 if (n==1 or n==0) else n * factorial(n - 1);

# Driver Code
num = 5;
print("Factorial of",num,"is",
factorial(num))

# This code is contributed by Smitha Dinesh Semwal


Iterative:

filter_none
edit
play_arrow

brightness_4
# Python 3 program to find
# factorial of given number
def factorial(n):

# single line to find factorial


return 1 if (n==1 or n==0) else n * factorial(n - 1);

# Driver Code
num = 5;
print("Factorial of",num,"is",
factorial(num))

# This code is contributed by Smitha Dinesh Semwal


One line Solution (Using Ternary operator):

filter_none
edit
play_arrow

brightness_4
# Python 3 program to find
# factorial of given number

def factorial(n):

# single line to find factorial


return 1 if (n==1 or n==0) else n * factorial(n - 1)

# Driver Code
num = 5
print ("Factorial of",num,"is",
factorial(num))

# This code is contributed


# by Smitha Dinesh Semwal.

You might also like