Showing posts with label factorial of a number in python. Show all posts
Showing posts with label factorial of a number in python. Show all posts

Wednesday, June 4, 2014

Factorial of a Number in Python


Recently I am trying to learn how to program in Python programming language one of the most programming problem is called Factorial of a Number. In factorial we refer to a number that is the product of all the integers from 1 to the the number given by our user. For example the factorial of 5! is 1*2*3*4*5 is equal to 120. When we talk about integer will refer this to a series of numbers that are whole number, negative,zero and positive number. Factorial numbers is not defined for negative number and zero will always gives you 1 as your value.

In this program I have written a simple factorial function to return factorial value result to the user of our program. As we can see in this function factorial we have one passing parameter we named it number and then an assignment statement solve_results which has an initial value of 1. We are also using for loop statement to perform the repetition of our commands and finally we have the solve_results that perform summation of values and it returns the computed value of our program.

def factorial(number):
    solve_results = 1
    for i in range(2, number + 1):
        solve_results *= i
    return solve_results
  

Sample Output of Our Program


Python Integrated Programming Environment

Program Listing

# Factorial of a Number 
# Written By: Mr. Jake R. Pomperada, MAED-IT
# Tools : Python
# Date  : June 4, 2014 Wednesday

def factorial(number):
    solve_results = 1
    for i in range(2, number + 1):
        solve_results *= i
    return solve_results
  
complete = False

while complete == False:

  print("@==================================@")
  print("      Factorial of a Number     ")
  print("@==================================@")
  print("");
  value = int(input("Kindly Enter a Number :=> "))
  print("");
  if value < 0:
    print("Sorry, factorial does not exist for negative numbers")
  elif value == 0:
    print("The factorial of 0 is 1")
  else:
    print("The factorial of",value,"is",factorial(value))
    print("")
  choice = input("Are You Finish?  Y/N: ").lower().strip()

  if choice == "y":
         complete = True
  else:
       print("")
  print("\n")
  print("Thank You For Using This Program.")