First Factorial Coderbyte Solution

0

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



Example 2:  


 Input: 8
 Output: 40320


first factorial coderbyte Java

import java.util.*;
import java.io.*;

class Main {

public static int FirstFactorial(int num) {
// code goes here
int fact=1;
for(int i=1;i<=num;i++){
fact*=i;
}
return fact;
}

public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
System.out.print(FirstFactorial(s.nextLine()));
}

}

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


def FirstFactorial(num):
# Set var sum to be 1
sum = 1
# For x in the range 2 to num+1, let sum be equal to sum multiplied by x ------- range(start, stop, step)
# E.g. if num = 3, sum will be 1 -> (1*2) -> (2*3) = 6
for x in range(2,num+1):
sum = sum * x
x += 1
return sum
print(FirstFactorial(input()))

first factorial coderbyte JavaScript

Using the JavaScript language, have the function FirstFactorial(num) take the num parameter being passed and return the factorial of it (ie. if num = 4, return (4 * 3 * 2 * 1)). For the test cases, the range will be between 1 and 18.  


function FirstFactorial(num) {

var tot = 1;
for (var i = 2; i <= num; i++) {
tot *= i;
}
return tot;
}

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.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !