64.6k views
2 votes
Assume the availability of a function called fact. The function receives an argument containing an integer value and returns an integer value. The function should return the factorial of the argument. That is, if the argument is one or zero, the function should return 1. Otherwise, it should return the product of all the integers from 1 to the argument. So the value of fact(4) is 1*2*3*4 and the value of fact(10) is 1*2*3*4*5*6*7*8*9*10. Additionally, assume that the variable k has been initialized with a positive integer value. Write a statement that assigns the value of fact(k) to a variable x. The solution must include multiplying the return value of fact by k.

User Soribel
by
4.0k points

1 Answer

5 votes

Answer:

Following are the code to the given question:

def fact(k):#defining a method fact that holds a parameter

o=1#defining a variable o that holds a value that is 1

if(k==0):#defining if block that checks k equal to 0

o=1#using o variable that holds a value 1

else:#defining else block

for i in range(k):#defining for loop to calculates Factorial

o=o*(i+1)#calculating Factorial value

return(o)#return Factorial value

k=10#defining a variable k that holds a value 10

print("Factorial value:", fact(k))#use print method tom print Factorial value

Output:

Factorial value: 3628800

Step-by-step explanation:

  • In this code, a method, "fact" is declared that accepts one parameter "k", in the method if block is used, in the if the block it checks k-value that is equal to 0 if the condition is true it will return a value that is "1".
  • Otherwise, it will go to the else block. This section, uses the for loop to calculates its factorial value and adds its value into the "o", and returns its value.
  • Outside the method, k is declared that holds its value and passes into the fact method parameter, and uses the print method to print its return value.
User Pablolmedorado
by
4.8k points