2

I am trying to make a calculator in python where you can add, subtract, multiply, divide, find the exponential, find the nth root of x and also for factorial of a number.

While trying to finish the calculator I realized there was a problem with calculating the factorial of a number with decimal as there is not thing called factorial of a number with a decimal.

Here is the code for factorial that I used.

a = float(input("Enter the first number: "))
ab = 1
ac = a
if a > 1:
    while a > 1:
         ab = ab*a
         a = a - 1
         print (ac,"factorial is",ab)
elif a == 0:
    print("0 factorial is 1")
else:
    print("There is no negetive factorial")

I tried to search for any code that could work out, but I still couldn't find any code for that.

I also tried to edit the above code like this.

a = float(input("Enter the first number: "))
ab = 1
ac = a
if not(a/1):
    print("there is no factorial of number with decimal")
else:
    if a > 1:
        while a > 1:
             ab = ab*a
             a = a - 1
             print (ac,"factorial is",ab)
    elif a == 0:
        print("0 factorial is 1")
    else:
        print("There is no negetive factorial")

Still, It doesn't do anything.

  • 1
    Not the answer you're after, but the gamma function is defined for real numbers and is very closely related – Sam Mason Jul 28 '23 at 16:35
  • A true online scientific calculator would not do your process. For any real `x`, the operation `x!` should give `Γ(x+1)` (gamma function). So, instead of this, why don't you use `math.gamma(a+1)` – Amolgorithm Jul 28 '23 at 17:22
  • 1
    You can use the [modulo operator](https://docs.python.org/3.3/reference/expressions.html#binary-arithmetic-operations), e.g. `if a % 1 > 0: print("there is no factorial of number with decimal")` – Ignatius Reilly Jul 28 '23 at 17:40
  • Yes, in this question I have found the way to check if the inputted integer is an integer or not. – Kailas Krishnaraj Jul 29 '23 at 12:27

2 Answers2

0

A true online scientific calculator would not do your process. For any real x, the operation x! should give Γ(x+1) (gamma function). So, instead of this, why don't you use math.gamma(a+1).

By doing this you would get the factorial for a, given that a is an integer.

Here is an example:

import math

a = float(input("Enter the first number: "))
print(math.gamma(a+1))

Here is a more detailed understanding of Gamma. The gamma function can be used to extend the factorial function to all complex numbers, and this is one of its primary uses.

This would be a simpler and more realistic way to handle factorial operations on a calculator. It's an alternative method to achieve a similar ultimate goal as yours.

Amolgorithm
  • 697
  • 2
  • 20
  • this actually works for making the whole factorial code smaller. But what I actually asked was of making a conditional statement to check whether the inputted number is a decimal or not. But anyway, I got an answer from another question. Also, would I face any problems if I don't export 'math'? – Kailas Krishnaraj Jul 29 '23 at 12:12
-2

if I understood you well, i tried this :

a = float(input("Enter the first number: "))
ab = 1
ac = a
if int(a) > 0:
    while a > 1:
        ab = int(ab*a)
        a = int(a) - 1
    print (int(ac),"factorial is",int(ab))
elif int(a) == 0:
    print("0 factorial is 1")
else:
    print("There is no negetive factorial")
Ayoub
  • 1
  • 1
  • 3