logo

Python-Programm zum Ermitteln der Fakultät einer Zahl

Was ist Fakultät?

Factorial ist eine nicht negative ganze Zahl. Es ist das Produkt aller positiven ganzen Zahlen, die kleiner oder gleich der von Ihnen gewünschten Fakultätszahl sind. Es wird durch ein Ausrufezeichen (!) gekennzeichnet.

Numpy-Punkt

Beispiel:

 n! = n* (n-1) * (n-2) *........1 4! = 4x3x2x1 = 24 

Der Faktorwert von 4 ist 24.

Hinweis: Der Faktorwert 0 ist immer 1. (Regelverstoß)

Beispiel -

 num = int(input(&apos;Enter a number: &apos;)) factorial = 1 if num <0: 0 print(' factorial does not exist for negative numbers') elif num="=" 0: print('the of is 1') else: i in range(1,num + 1): of',num,'is',factorial) < pre> <p> <strong>Output:</strong> </p> <pre> Enter a number: 10 The factorial of 10 is 3628800 </pre> <p> <strong>Explanation -</strong> </p> <p>In the above example, we have declared a <strong>num</strong> variable that takes an integer as an input from the user. We declared a variable factorial and assigned 1. Then, we checked if the user enters the number less than one, then it returns the factorial does not exist for a negative number. If it returns false, then we check num is equal to zero, it returns false the control transfers to the else statement and prints the factorial of a given number.</p> <h3>Using Recursion</h3> <p>Python recursion is a method which calls itself. Let&apos;s understand the following example.</p> <p> <strong>Example -</strong> </p> <pre> # Python 3 program to find # factorial of given number def fact(n): return 1 if (n==1 or n==0) else n * fact(n - 1); num = 5 print(&apos;Factorial of&apos;,num,&apos;is&apos;,) fact(num)) </pre> <p> <strong>Output:</strong> </p> <pre> Factorial of 5 is 120 </pre> <p> <strong>Explanation -</strong> </p> <p>In the above code, we have used the recursion to find the factorial of a given number. We have defined the <strong>fact(num)</strong> function, which returns one if the entered value is 1 or 0 otherwise until we get the factorial of a given number.</p> <h3>Using built-in function</h3> <p>We will use the math module, which provides the built-in <strong>factorial()</strong> method. Let&apos;s understand the following example.</p> <p> <strong>Example -</strong> </p> <pre> # Python program to find # factorial of given number import math def fact(n): return(math.factorial(n)) num = int(input(&apos;Enter the number:&apos;)) f = fact(num) print(&apos;Factorial of&apos;, num, &apos;is&apos;, f) </pre> <p> <strong>Output:</strong> </p> <pre> Enter the number: 6 Factorial of 6 is 720 </pre> <p>We have imported the math module that has <strong>factorial()</strong> function. It takes an integer number to calculate the factorial. We don&apos;t need to use logic.</p> <hr></0:>

Erläuterung -

Im obigen Beispiel haben wir a deklariert Auf eins Variable, die eine Ganzzahl als Eingabe vom Benutzer entgegennimmt. Wir haben eine Variable „Fakultät“ deklariert und ihr den Wert 1 zugewiesen. Dann haben wir überprüft, ob der Benutzer eine Zahl kleiner als eins eingibt. Dann wird zurückgegeben, dass die Fakultät für eine negative Zahl nicht existiert. Wenn es „Falsch“ zurückgibt, überprüfen wir, ob „num“ gleich Null ist, es gibt „Falsch“ zurück, die Steuerung geht an die else-Anweisung über und gibt die Fakultät einer bestimmten Zahl aus.

Rekursion verwenden

Die Python-Rekursion ist eine Methode, die sich selbst aufruft. Lassen Sie uns das folgende Beispiel verstehen.

Beispiel -

 # Python 3 program to find # factorial of given number def fact(n): return 1 if (n==1 or n==0) else n * fact(n - 1); num = 5 print(&apos;Factorial of&apos;,num,&apos;is&apos;,) fact(num)) 

Ausgabe:

Liste sortieren Java
 Factorial of 5 is 120 

Erläuterung -

Im obigen Code haben wir die Rekursion verwendet, um die Fakultät einer bestimmten Zahl zu ermitteln. Wir haben das definiert Tatsache Funktion, die eins zurückgibt, wenn der eingegebene Wert 1 oder 0 ist, andernfalls bis wir die Fakultät einer bestimmten Zahl erhalten.

Verwendung der integrierten Funktion

Wir werden das Mathematikmodul verwenden, das die integrierten Funktionen bereitstellt Fakultät() Methode. Lassen Sie uns das folgende Beispiel verstehen.

Beispiel -

 # Python program to find # factorial of given number import math def fact(n): return(math.factorial(n)) num = int(input(&apos;Enter the number:&apos;)) f = fact(num) print(&apos;Factorial of&apos;, num, &apos;is&apos;, f) 

Ausgabe:

 Enter the number: 6 Factorial of 6 is 720 

Wir haben das Mathematikmodul importiert, das vorhanden ist Fakultät() Funktion. Zur Berechnung der Fakultät wird eine ganze Zahl benötigt. Wir müssen keine Logik verwenden.