C++ Program to Find Factorial of a Number using Dynamic Programming

In this C++ program, we will have a look at the C++ Program to Find Factorial of a Number using Dynamic Programming.

C++ Program to Find Factorial of a Number using Dynamic Programming

#include <iostream>
using namespace std;
int result[1000] = {0};
int fact(int num) {
if (num >= 0) {
result[0] = 1;
for (int i = 1; i <= num; ++i) {
result[i] = i * result[i - 1];
}
return result[num];
}
}
int main() {
int num;
while (1) {
cout<<"Please enter a number:";

cin>>num;

if (num == 0)

break;

cout<<"Factorial of a number is:"<<fact(num)<<endl;

}

return 0;

}

Output:

Please enter a number: 5

Factorial of a number is: 120


		

Leave a Reply

Your email address will not be published. Required fields are marked *

Shopping cart