## Interview Question

Country: United States

Comment hidden because of low score. Click to expand.
2
of 2 vote

``````import org.junit.Test;

import java.util.stream.IntStream;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class Fact001 {
public int fac(int n) {
return n == 1 ? 1 : n * fac(n-1);
}

public int[] all(int n) {
return IntStream.rangeClosed(1, n).map(i -> fac(i)).toArray();
}

@Test
public void test() {
assertThat(all(3), is(new int[]{1,2,6}));
}

}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

int fac(int n) {
int ans;
if(n==1) {
printf ("1 : %d",n);
return 1;
}
ans = n * fac(n-1);
printf ("%d : %d",n,ans);
return ans;
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

int fact(int x){
if(x==0)
return 1;
else
return x * fact(x-1);
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````{{

import java.util.Scanner;

public class FactorialNum {

public static void main(String[] args) {

long temp, temp1;
Scanner input = new Scanner(System.in);
System.out.println("Enter the integer yo want to have factorial: ");
temp = input.nextLong();
temp1 = thatFact(temp);
System.out.println(temp1);

}

public static long thatFact(long test) {

long temp = 1, i, k;
for (i = 0; i <= test; i++) {
temp = test * (test - 1);
for (k = (test - 1); k > 1; k--) {
temp = temp * (k - 1);
}
}
return temp;
}``````

}}

Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{{{{ import java.util.Scanner; public class FactorialNum { public static void main(String[] args) { long temp, temp1; Scanner input = new Scanner(System.in); System.out.println("Enter the integer yo want to have factorial: "); temp = input.nextLong(); temp1 = thatFact(temp); System.out.println("The Factorial of the given number " + temp + " is : "+temp1); } public static long thatFact(long test) { long temp = 1, i, k; for (i = 0; i <= test; i++) { temp = test * (test - 1); for (k = (test - 1); k > 1; k--) { temp = temp * (k - 1); } } return temp; } } }}}}}}}}
Comment hidden because of low score. Click to expand.
0
of 0 vote

``````int fact(int n)
{
if(n<0)
return 0;
return n == 0 ? 1 : n * fact(n-1);
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````class Solution(object):
def fac(self,n):
if n == 1 or n == 0:
return 1
return n*self.fac(n-1)``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

int factorial(int n){

if(n==0){
printf("\nfactorial of 0 = 1");
return 1;
}

else{
int fact = n * factorial(n-1);
printf("\nfactorial of %d = %d",n,fact);
return fact;

}

}

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.