function will execute infinite time and stackoverflow occur,
base case must be if(n==0||n==1) return 1;
Base case is wrong for n==0. it will display stackoverflow
Hey @Vipin_coder
base case :
if(n==0) {
return n;
}
fact is always 0
if(n==0||n==1) return n; // No need
simple use
if(n==1) return n;
Thank you so much sir
Please mark your doubts as resolved in your course’s.
but you told, if (n==0) return n , fact is always 0, how it can be happened.
@Vipin_coder
public static int fact(int n) {
if(n==0) {
return n;
}
return n*fact(n-1);
}
this is a fac function
retutn n * fact(n-1); // base case se 0 aaya
n *0 its 0
I would suggest you Make call Stack .
but factorial of zero must be 1, not 0
your base case is wrong
if is n == 0 , you return 1.its is correct
A/c this base Case its give Zero
yes but your base case will return 0 for 0 , and the base case in lecture is if(n==1) return 1, when i will put n==0, then stackoverflow will occur
Yes this code is wrong.
I have responded this Question
you told, if (n==0) return n , fact is always 0, how it can be happened.
i mean (n==0||n==1) return 1, but you said fact is always 0 for 0.
you mean for n=0 , factorial is 0.
ok , but when i will enter 0 for n then stack overflow will occur , so this base case is wrong
Mai 0 pe nhi jaaunga.
n always n-1 ho raha h
pehle base case n==1 yaha se return kr jayenge 1
0 wala case kabhi nhi aayega