Have the function FirstFactorial(num) take the num parameter being passed and return the factorial of it.
For example: if num = 4, then your program should return (4 * 3 * 2 * 1) = 24. For the test cases, the range will be between 1 and 18 and the input will always be an integer.
Example 1:
Input: 4
Output: 24
Input: 8
Output: 40320
first factorial coderbyte Java
Explanation
FirstFactorial is a method which accept one int parameter called num that is the value we need to find the factorial. We initialize one more variable fact where we store the product of the entire numbers which comes under the num. After the completion of the loop, the final fact value will be the factorial of that number. We return that fact as method output and print it on the console.
first factorial coderbyte Python
first factorial coderbyte JavaScript
Explanation:
You can either use an iterative or recursive function to solve this challenge. I am going to use an interative function. I am going to start with a value of 1 for my total and then keep multiplying it by the next number until I reach num.
This function needs to account for a possible outlier - One and Zero.
If num is 1 or 0 then the answer is 1. By setting tot to value of 1 at
initialization, then it guaranteees that 1 will be returned if num is ever 0 or 1.
Steps for solution
1) Set var tot to 1.
2) Loop from 2 to num and multiple tot by num to get new tot.
3) Return tot for answer.
Disclaimer: The above Problem is generated by Coderbyte but the Solution is provided by ShouterFolk.