Class: Intelligent Systems; Find the factorial using recursive function. factori
ID: 3852063 • Letter: C
Question
Class: Intelligent Systems; Find the factorial using recursive function. factorial(0,1).
factorial(A,B):-
A > 0,
C is A-1,
factorial(C,D)
B is A*D.
Explanation / Answer
C++ Program:
#include<iostream>
using namespace std;
int factorial(int n);
class IntelligentSystems
{
public:
int factorial(int n)
{
if(n > 1)
return n * factorial(n - 1);
else
return 1;
}
};
int main()
{
IntelligentSystems obj;
int n;
cout << "Enter a positive integer: ";
cin >> n;
cout << "Factorial of " << n<<" is "<<obj.factorial(n);
return 0;
}
Output:
Enter a positive integer: 5
Factorial of 5 is 120