Question

In: Computer Science

Create the function fact that had a positive integer argument and returns its factorial . Recall...

Create the function fact that had a positive integer argument and returns its factorial . Recall that factorial (n) = 1*2*3***n. For example factorial (4)=1*2*3*4=24, (python)

Solutions

Expert Solution

Factorial is the product of all positive integer which is less than given number is known as factorial of that number for example we can say 24 is the factorial of number 4.

24 = 1*2*3*4;

here i wrote the code in python 3. and after this // i will comment about line and also comment will in green color and code in black color :

def fact(val):    // this is the function declartion with parameter which we want to pass so here i am passing val parameter to this function.
    total=1     // here i take total variable in which i will store factorial value.
    for i in range(1,val+1):     // for loop for factorial calculation here i am taking range 1 to val+1 (where val is the value which we give to calculate factorial ) because 1 will be starting point of for loop but if we give only val as stop point so python will take val - 1 as a stop point so i give val + 1.
        total=total * i     // here our loop will run at val times and it will calcutate factorial
    return total         // now function will return factorial value which is we stored in total.
val = int(input("enter a number"))     // here we are taking user input and storing to val. and here also i added int so it will take only integer value.
print("(",val,") =",end=" ")       // from this line it will print ( val ) =
for i in range(1,val):      // in this loop we will get output of sequence and multiply symbol until value-1 which we will enter.
    print(i,"*",end=" "),
print(val," = ",fact(val))   // now finally we call the function by fact(val) and before this we will print number which we will enter because above for loop print till val -1 and then after function call function give factorial and which will print.


Related Solutions

Python: Write a function named calc_odd_sum that accepts a positive integer as the argument and returns...
Python: Write a function named calc_odd_sum that accepts a positive integer as the argument and returns the sum of all the odd numbers that are less than or equal to the input number. The function should return 0 if the number is not positive. For example, if 15 is passed as argument to the function, the function should return the result of 1+3+5+7+9+11+13+15. If -10 is passes, it shall return 0. You shall run the program test the result at...
Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as
Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as: n! = n·(n-1)·(n-2)·.……·3·2·1. The factorial of 0 is 1. If a number is negative, factorial does not exist and this program should display error message. Sample output dialogues should look like these:  Enter an integer: -5 ERROR!!! Tactorial of a negative number doesn't exist  Enter an integer: 10  Factorial = 3628800
python exercise: a. Write a function sumDigits that takes a positive integer value and returns the...
python exercise: a. Write a function sumDigits that takes a positive integer value and returns the total sum of the digits in the integers from 1 to that number inclusive. b. Write a program to input an integer n and call the above function in part a if n is positive, else give ‘Value must be Positive’ message. sample: Enter a positive integer: 1000000 The sum of the digits in the number from 1 to 1000000 is 27000001 Enter a...
Write a Scheme function that takes a positive integer n and returns the list of all...
Write a Scheme function that takes a positive integer n and returns the list of all first n positive integers in decreasing order: (decreasing-numbers 10) (10 9 8 7 6 5 4 3 2 1) Please explain every step
using the fact that : A positive integer n ≥ 3 is constructive if it is...
using the fact that : A positive integer n ≥ 3 is constructive if it is possible to construct a regular n-gon by straightedge and compass, it is possible to construct the angle 2π/n. And that if both angles α and β can be constructed by straightedge and compass then so are their sums and differences.The outside angle of a regular n-gon is 2π/n. 1. Suppose that n = p^(α1) ··· p^(αk) where p ,··· , pk are distinct odd...
Using C++ Write a template function that accepts an integer parameter and returns its integer square...
Using C++ Write a template function that accepts an integer parameter and returns its integer square root. The function should return -1, if the argument passed is not integer. Demonstrate the function with a suitable driver program .
Python 3 Calculate factorial Create a function that uses a loop to calculate the factorial of...
Python 3 Calculate factorial Create a function that uses a loop to calculate the factorial of a number. * function name: get_fact * parameters: num (int) * returns: int * operation: Must use a loop here. Essentially calculate and return the factorial of whatever number is provided. but: - If num is less than 1 or greater than 20, print "num is out of range" and exit the function - If num is not an int, print "invalid num parameter"...
Create a method that takes a HashMap<Integer, Integer> and returns the sum of the keys of...
Create a method that takes a HashMap<Integer, Integer> and returns the sum of the keys of the HashMap.
(EXCEL) Create a user-defined function called Hexagon that takes one argument called side and returns the...
(EXCEL) Create a user-defined function called Hexagon that takes one argument called side and returns the area of a regular hexagon given the length of the side. Show that the function works in a worksheet by inserting the formula somewhere.
Create an integer to std_logic_vector function(i2std)
Create an integer to std_logic_vector function(i2std)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT