0

If you send fact(0) it will breack?

In the exaple it use: int factorial(int n) { if (n==1) { return 1; } else { return n * factorial(n-1); } } I think that line of code should break and we need to use a conditional in that case.

8th Nov 2016, 12:11 PM
Marco Capo
Marco Capo - avatar
7 Réponses
+ 1
It will spew out nonsense, yeah. The numbers will go into the negatives until n is so small that it wraps around (integer overflow) at which point it will become huge, and then n will continue decreasing until it is 1. You should probably add a "if(n<1)" check, just to catch everything :) For a quick example the code is fine though.
8th Nov 2016, 12:16 PM
Schindlabua
Schindlabua - avatar
+ 1
You should use if(n<=1) return 1;
8th Nov 2016, 1:43 PM
Aditya kumar pandey
Aditya kumar pandey - avatar
0
you can use: if(n==0) return 1; // this will include '0' as well.
8th Nov 2016, 7:07 PM
kamal joshi
kamal joshi - avatar
0
so as I suspected the c++ example in the lesson has a bug
8th Nov 2016, 7:25 PM
Marco Capo
Marco Capo - avatar
0
I guess they are not expecting us to find 0!
8th Nov 2016, 7:27 PM
kamal joshi
kamal joshi - avatar
0
thats how Bugs happen
8th Nov 2016, 7:29 PM
Marco Capo
Marco Capo - avatar
0
very true
8th Nov 2016, 7:30 PM
kamal joshi
kamal joshi - avatar