More

    Python Program to Find Factorial of a Number Using FOR Loop

    The factorial of a number is the product of all the positive integers less than or equal to the number. For example, the factorial of 5 is = 5 x 4 x 3 x 2 x 1 = 120.

    Program Code

    num = int(input("Enter a number: "))
    factorial = 1
    
    if num < 0:
       print("Sorry, factorial does not exist for negative numbers")
    elif num == 0:
       print("The factorial of 0 is 1")
    else:
       for i in range(1,num + 1):
           factorial = factorial*i
       print("The factorial of",num,"is",factorial)

    Input Given:

    Enter a number: 9

    Expected Output:

    The factorial of 9 is 362880

    Code Explanation

    1. We first check if the number is negative.
    2. If the number is negative we display an appropriate message.
    3. If the number is positive, we use a for loop to calculate the factorial.
    4. We then display the factorial of the number.
    Disclaimer: While we make every effort to update the information, products, and services on our website and related platforms/websites, inadvertent inaccuracies, typographical errors, or delays in updating the information may occur. The material provided on this site and associated web pages is for reference and general information purposes only. In case of any inconsistencies between the information provided on this site and the respective product/service document, the details mentioned in the product/service document shall prevail. Subscribers and users are advised to seek professional advice before acting on the information contained herein. It is recommended that users make an informed decision regarding any product or service after reviewing the relevant product/service document and applicable terms and conditions. If any inconsistencies are observed, please reach out to us.

    Latest Articles

    Related Stories

    Leave A Reply

    Please enter your comment!
    Please enter your name here

    Join our newsletter and stay updated!