Python Program do znajdowania silni liczby
โก Inteligentne podsumowanie
Factorial of a number in Python is the product of every positive integer up to that number. Python computes it with a for loop, an if-else check, recursion, or the built-in math.factorial() function.

The sections below show four ways to compute a factorial in Python โ a for loop, an if-else version, recursion, and math.factorial() โ plus the underlying algorithm and applications.
Silnia liczby przy uลผyciu pฤtli for
Let us take up the example of Python code that takes a positive integer as input to determine the factorial of positive integers. In the following code, the loop begins with one, and then it multiplies by each number that precedes the actual number whose factorial is to be determined.
Poniลผszy Python code illustrates the factorial function using a loop.
Python kod:
print ("Input a number") factorialIP = int (input ()) ffactor23 = 1 for j in range (1, factorialIP+1): ffactor23 = ffactor23 * j print ("The factorial of the number is โ, ffactor23)
Wyjลcie:
Input a number
4
The factorial of the number is 24
Powyลผej Python program takes the input of positive numbers only, and it does not have a check of negative numbers in it. In this program, the factor is 1 when j is equal to 1. When j is 2, the factor is multiplied with 2, and it will do the action till j comes at 4 to arrive at 24.
Silnia liczby przy uลผyciu instrukcji IFโฆelse
Poniลผszy Python code illustrates the factorial function using a function. Unlike the loop version, this program also checks for negative numbers before computing the factorial.
W poprzednim Python code, the check for negative numbers was not applied, making the factorial function incomplete and prone to deliver an error message if negative numbers are given as an input.
In the given code, the loop begins with one and multiplies by each preceding number, and the function also validates the input for negative numbers.
Python kod:
print("Enter a number for the purpose of determining factorial") factorialIP = int(input()) def factorial(factorialIP): if factorialIP < 0: print ('Factorial does not exist') factor=0 return factor elif factorialIP == 0: factor=1 return factor print(factor) else: factor = 1 for j in range (1, factorialIP+1): factor = factor * j return factor print ("The factorial of the number is ", factorial(factorialIP))
Wyjลcie:
1) Enter a number to determine factorial -4 Factorial does not exist The factorial of the number is 0 2) Enter a number to determine factorial 4 Factorial does not exist The factorial of the number is 24
Ten Python program accepts positive numbers and adds a check for negative numbers using the if and else statement, correctly returning 24 for an input of 4.
Silnia liczby przy uลผyciu rekurencji
Poniลผszy Python code illustrates the factorial function using recursion. In this example, a recursive function that takes a positive integer as input determines the factorial number.
Python kod:
print("Enter a number for the purpose of determining factorial") def factorial(num2): if num2 < 0: return 'Factorial does not exist' elif num2 == 0: return 1 else: return num2 * factorial(num2-1) number1 = int(input()) print("The factorial of the number is",factorial(number1))
Wyjลcie:
Enter a number for the purpose of determining factorial 4 The factorial of the number is 24
The recursion can be explained as a concept wherein the function invoked in the Python module can call itself again and again. It runs until the Python condition present in the module is satisfied, wherein the function invoked is passed with a value.
W powyลผszym Python program, the function def factorial keeps calling itself recursively until and unless the number reaches zero. Once the number reaches zero, it initializes the number as 1, ending the recursion.
Factorial of a Number using math.factorial()
Poniลผszy Python code illustrates the factorial function using math.factorial(), which can be used by importing the math module.
This function does not accept negative integers, and it throws a value error when float numbers are provided.
Python kod:
print("Enter a number for computing factorial") import math number1 = int(input()) print("The factorial is as computed comes out to be ") print(math.factorial(number1))
Wyjลcie:
Enter a number for computing factorial 4 The factorial, as computed, comes out to be 24
Algorytm programu silniowego w Python
Weลบmy przykลad ilustrujฤ cy koncepcjฤ silni.
For determination of factorial 5, follow these steps:
5! = 5 x (5-1) x (5-2) x (5-3) x (5-4) 5! =120
Tutaj, 5! wyraลผa siฤ jako 120.
Poniลผszy diagram pomaga zrozumieฤ algorytm obliczania silni. W tym przypadku weลบmiemy przykลad silni 4!
Algorytm z obrazowym przykลadem silni 4!
Zastosowanie silni w Python
Silnia liczby ma szerokie zastosowanie w matematyce. Oto waลผne zastosowania Python:
- Python helps in computation, followed with print factorial in faster and more efficient terms than other available programming languages.
- Python code is easily understandable and can be replicated across different platforms, and the factorial Python program can be incorporated in several mathematical model-building assignments.

