Python Forum
Python Program to Find the Factorial of a Number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Program to Find the Factorial of a Number
#1
# Python program to find the factorial of a number provided by the user.

# change the value for a different result
num = 7

# To take input from the user
#num = int(input("Enter a number: "))

factorial = 1

# check if the number is negative, positive or zero
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)
Yoriz write Nov-21-2022, 09:24 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Line 17, change both '1' to 'i' line 17
Reply
#3
Use a function:
def factorial(num):
    factorial = 1

    if num < 0:
        print("Sorry, factorial does not exist for negative numbers")
    else:
        for i in range(1, num + 1):
            factorial *= i
        print("The factorial of", num, "is", factorial)
With math.factorial it's easier:
def factorial(num):
    if num < 0:
        print("Sorry, factorial does not exist for negative numbers")
    else:
        print("The factorial of", num, "is", math.factorial(num))
A function that annoys the user until he has entered an integer:
import math


def factorial():
    max_value = 2_000

    while True:
        user_input = input("Factorial of: ")

        try:
            value = int(user_input)
        except ValueError:
            print("A valid integer is required")
            continue

        if value < 0:
            print("Negative integers are not allowed")
            continue

        if value > max_value:
            print(f"Integer is bigger than {max_value}")
            continue

        break

    print(math.factorial(value))
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug Program to check whether a number is palindrome or not PythonBoy 18 2,725 Sep-12-2023, 09:27 AM
Last Post: PythonBoy
  Trying to find the next prime number FirstBornAlbratross 8 4,360 Aug-14-2023, 01:16 PM
Last Post: deanhystad
  Program that allows to accept only 10 integers but loops if an odd number was entered gachicardo 4 3,635 Feb-24-2022, 10:40 AM
Last Post: perfringo
  How to find the accuracy vs number of neighbours for KNN vokoyo 3 3,183 Apr-10-2019, 03:46 AM
Last Post: scidam
  Program that displays the number with the greatest amount of factors ilusmd 3 2,829 Nov-01-2018, 08:28 PM
Last Post: ichabod801
  Fork the process (factorial) vndywarhol 3 3,416 Sep-07-2018, 03:28 AM
Last Post: ichabod801
  Help on Creating Program to find the Median and Mode by hand EvanCahill 3 2,935 Jul-19-2018, 06:17 PM
Last Post: woooee
  python age calculator need to find the number of years before they turn 100 not using orangevalley 4 10,006 Mar-26-2018, 04:44 AM
Last Post: PyMan
  Program: count and find Truman 3 4,664 Feb-11-2018, 11:06 PM
Last Post: Larz60+
  how to find a next prime number? iamyourfather 2 6,527 Oct-01-2017, 04:21 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020