Question
i need them seprately
public static int factorial ( int n ) Return n factorial, n factorial is the product of all positive integers up to n. For instance, factorial(5) should return 1*2*3*4*5 == 120. (Factorials of negative integers are not meaningful, you don't need to worry about them.) public static long fibonacci (int n ) Return the Fibonacci function of n. The Fibonacci function starts with Fibonacci(0) = 0, Fibonacci(1) = 1, and thereafter the Fibonacci function of a number is the sum of the Fibonacci function of the preceding 2 numbers. So, Fibonacci(0) = 0 Fibonacci(1) = 1 Fibonacci(2) = 1 Fibonacci(3) = 2 Fibonacci(4) = 3 Fibonacci(5) = 5 Fibonacci(6) = 8, and so on
Explanation / Answer
public static int factorial(int n)
{
int fact=1;
for(int i=1;i<=n;i++) fact=fact*i;
return fact;
}
public static long Fibonacci(int n)
{
long f1=0,f2=1,count=1;
if(n<=0) return 0;
else if(n==1) return 1;
else{
while(count < n)
{
count++;
long t=f1+f2;
f1=f2;
f2=t; // f2 stores fibonacci(count)
}
return f2;
}
}