The factorial of a positive integer n is the product of all positive integers less than or equal to n. The factorial of a number is represented by the symbol "!" . For example, the factorial of 5 is 5 * 4 * 3 * 2 * 1 = 120.

Here is a Python function that calculates the factorial of a given number using a for loop:

def factorial(n):
    if n < 0:
        return None
    if n == 0:
        return 1
    result = 1
    for i in range(1, n+1):
        result *= i
    return result

This function takes a single argument, n, and returns the factorial of n. It first checks if n is less than 0, if so it returns None. If n is equal to 0, it returns 1 (since the factorial of 0 is defined to be 1). It then initializes a variable named result to 1, then uses a for loop to iterate over the range of integers from 1 to n inclusive and multiply each number to result variable. Finally, the function returns the value of result, which is the factorial of n.

You can call this function and pass in any positive integer to calculate its factorial:

>>> factorial(5)
120
>>> factorial(3)
6
>>> factorial(10)
3628800

Alternatively python has math.factorial function which you can use without writing your own function.

import math
math.factorial(5)

Do note that factorial of number can be very large, even for relatively small numbers and python integers may not be large enough to store those values.

#1: Python program to find factorial of a given number without using recursion

  1. #Factorial without recursion

  2. n=int(input("Enter the number: "))
  3. fact=1
  4. if n<0:
  5.     print("factorial doesn't exist for negative numbers")
  6. else:
  7.     for i in range(1,n+1):
  8.         fact=fact*i
  9.     print("factorial of", n, "is", fact)

Output:

  1. Enter the number: 5
  2. factorial of 5 is 120





You Might like :
  1. Python try except else with example program
  2. Python finally block example with program
  3. Python try-except- else vs finally with example program
  4. Find factorial of a number without using recursion using python
  5. Python program to find factorial without recursion
  6. If statement in python example program
  7. Else statement in python with example program
  8. Types of operators in python
  9. python math operators
  10. python division operator
  11. Python modulo operator
  12. Python bitwise operators
  13. python comparison operators
  14. Logical operators in python
  15. Assignment operator in python
  16. Identity operator in python
  17. Python power operator
  18. Not equal operator in python
  19. python not operator
  20. python and operator
  21. python Ternary operator
  22. python string operations
  23. python string methods
  24. Spilt function in python
  25. Format function in python
  26. String reverse in python
  27. python tolower
  28. Remove spaces from string python
  29. Replace function in python
  30. Strip function in python
  31. Length of string python
  32. Concat python
  33. startswith python
  34. strftime python
  35. is numeric python
  36. is alpha python
  37. is digit python
  38. python substring


Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

1 comments for Python program to find factorial without recursion

Select Menu