+ 2

Hey friends please explain me this code and say why its output is 4?

include <iostream> using namespace std; int fact (int n) { if(!n) return 1; return n*fact(n-1); } int main() { cout<<fact(3)-fact(2); return 0; } //output is 4 but why ?

13th Sep 2018, 7:46 AM
Albert Whittaker :Shaken To Code
Albert Whittaker :Shaken To Code - avatar
1 Odpowiedź
+ 1
The function fact returns the factorial of n - "the product of all positive integers less than or equal to n". fact(3) = 3 * 2 * 1 = 6 fact(2) = 2 * 1 = 2 fact(3) - fact(2) = 4
13th Sep 2018, 8:15 AM
Hoàng Nguyễn Văn
Hoàng Nguyễn Văn - avatar