Learning coding means GreatToCode Be more than a Coder ! Greattocode , Join GreatToCode Community,1000+ Students Trusted On Us .If You want to learn coding, Then GreatToCode Help You.No matter what It Takes !


CODE YOUR WAY TO A MORE FULFILLING And HIGHER PAYING CAREER IN TECH, START CODING FOR FREE Camp With GreatToCode - Join the Thousands learning to code with GreatToCode
Interactive online coding classes for at-home learning with GreatToCode . Try ₹Free Per Month Coding Classes With The Top Teachers . five Python programs to find the factorial of a given number

five Python programs to find the factorial of a given number

Become More Then A coder | Learn & Start Coding Now.

Here are five Python programs to find the factorial of a given number using different approaches:


### 1. Using Iterative Approach:

```python
# Program 1: Finding factorial using iterative approach
def factorial_iterative(n):
    result = 1
    for i in range(1, n + 1):
        result *= i
    return result

number = 5
print(f"The factorial of {number} is:", factorial_iterative(number))
```

### 2. Using Recursive Approach:

```python
# Program 2: Finding factorial using recursive approach
def factorial_recursive(n):
    if n == 0:
        return 1
    else:
        return n * factorial_recursive(n - 1)

number = 5
print(f"The factorial of {number} is:", factorial_recursive(number))
```

### 3. Using Math Module:

```python
# Program 3: Finding factorial using math module
import math

number = 5
factorial = math.factorial(number)
print(f"The factorial of {number} is:", factorial)
```

### 4. Using Reduce Function:

```python
# Program 4: Finding factorial using reduce function
from functools import reduce

number = 5
factorial = reduce(lambda x, y: x * y, range(1, number + 1))
print(f"The factorial of {number} is:", factorial)
```

### 5. Using While Loop:

```python
# Program 5: Finding factorial using while loop
def factorial_while(n):
    result = 1
    while n > 0:
        result *= n
        n -= 1
    return result

number = 5
print(f"The factorial of {number} is:", factorial_while(number))
```

Each of these programs calculates the factorial of a given number using a different approach - iterative, recursive, using the math module, with the reduce function, and with a while loop. Feel free to run these programs with different values to compute the factorial for any desired number.

Post a Comment

0 Comments

•Give The opportunity to your child with GreatToCode Kid's • Online Coding Classes for Your Kid • Introduce Your kid To the world's of coding
•Fuel You Career with our 100+ Hiring Partners, Advance Your Career in Tech with GreatToCode. •Join The Largest Tech and coding Community and Fast Forward Your career with GreatToCode. •10000+ Learner's+ 90 % placement Guarantee. • Learning Coding is Better with the GreatToCode community .
•Greattocode Kid's •GreatToCode Career •GreatToCode Interview •GreatToCode Professional •GreatToCode for schools •GreatToCode For colleges •GreatToCods For Businesses.
Are you ready to join the millions of people learning to code? GreatToCode Pass is your one-stop-shop to get access to 1000+ courses, top-notch support, and successful job placement. What are you waiting for? Sign up now and get your future in motion with GreatToCode Pass.