Knowee
Questions
Features
Study Tools

FactorialWrite a python function to find the factorial of given numberConstraints:4 < n < 20print -1 otherwiseInput format :Integer valueOutput format :Integer value

Question

FactorialWrite a python function to find the factorial of given numberConstraints:4 < n < 20print -1 otherwiseInput format :Integer valueOutput format :Integer value

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a Python function that calculates the factorial of a given number within the constraints you provided:

def factorial(n):
    # Check if the number is within the constraints
    if n < 4 or n > 20:
        return -1
    else:
        # Initialize the factorial to 1
        fact = 1
        # Calculate the factorial
        for i in range(1, n + 1):
            fact *= i
        return fact

You can use this function like this:

print(factorial(5))  # Output: 120
print(factorial(21))  # Output: -1

This function first checks if the input number n is within the constraints (4 < n < 20). If it's not, the function returns -1. If it is, the function calculates the factorial by initializing a variable fact to 1 and then multiplying it by every number from 1 to n in a loop. The result is then returned.

This problem has been solved

Similar Questions

Write a python function to find the factorial of given number

Factorial of a Number Send Feedback You are given an integer ‘N’. You have to print the value of Factorial of ‘N’. The Factorial of a number ‘N’ is defined as the product of all numbers from 1 to ‘N’. For Example: Consider if ‘N’ = 4, the Factorial of 4 will be the product of all numbers from 1 to 4, which is 1 * 2 * 3 * 4 = 24. Hence, the answer is 24. Input Format: The test case contains one single integer ‘N’ representing the given integer. Output Format: Print the value of the factorial of ‘N’.

Python Program to Find the Factorial of a Number Without Recursion

Write a program to calculate the factorial of N.

Write a Shell script to find Factorial of a given number.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.