No products in the cart.

Write a python program using functions …

CBSE, JEE, NEET, CUET

CBSE, JEE, NEET, CUET

Question Bank, Mock Tests, Exam Papers

NCERT Solutions, Sample Papers, Notes, Videos

Write a python program using functions to find the factorial of any given number.
  • 1 answers

Gaurav Seth 3 years, 8 months ago

Find factorial using Python function – using for loop

#Pyton program to find factorial of a number

def factorial(num):#function definition

fact=1

for i in range(1, num+1):#for loop for finding factorial

fact=fact*i

return fact #return factorial

number=int(input("Please enter any number to find factorial: "))

result=factorial(number)#function call and assign the value to variable result

print("The factorial of %d = %d"%(number,result))

 

When the above code is executed, it produces the following results

Please enter any number to find factorial: 6

The factorial of 6 = 720

 

Find factorial using Python function – using while loop

def factorial(num):#function definition

fact=1;

i=1

while i<=num:

fact=fact*i

i=i+1

return fact

num=input("Enter a number..");

result=factorial(num)

print("factorial of the number: %d" %result)

factorial(num)#function call

When the above code is executed, it produces the following results

Enter a number..5

factorial of the number: 120

 

Programming approaches for implementing these program

  • Take a number as input  from the user and stored in variable number for finding factorial
  • Declare a python function to find factorial
  • Inside the function, declare the variable fact and initialise as 1
  •  Inside the function, find the factorial of a given number using for loop in Python
  • Run the loop from given number until 1 and multiply numbers
  • Call the factorial() function and assign the output to variable result
  • the factorial of the given number is displayed using the print() function in Python
http://mycbseguide.com/examin8/

Related Questions

What are called tokens
  • 2 answers

myCBSEguide App

myCBSEguide

Trusted by 1 Crore+ Students

Test Generator

Test Generator

Create papers online. It's FREE.

CUET Mock Tests

CUET Mock Tests

75,000+ questions to practice only on myCBSEguide app

Download myCBSEguide App