C++ Program to Find Factorial of Large Numbers


The following is an example to find the factorial.

Example

#include <iostream>
using namespace std;
int fact(unsigned long long int n) {
   if (n == 0 || n == 1)
   return 1;
   else
   return n * fact(n - 1);
}
int main() {
   unsigned long long int n;
   cout<<"Enter number : ";
   cin>>n;
   cout<< “\nThe factorial : “ << fact(n);
   return 0;
}

Output

Enter number : 19
The factorial : 109641728

In the above program, we have declared a variabe with the following data type for large numbers.

unsigned long long int n;

The actual code is in fact() function as follows −

int fact(unsigned long long int n) {
   if (n == 0 || n == 1)
   return 1;
   else
   return n * fact(n - 1);
}

In the main() function, a number is entered by the user and fact() is called. The factorial of entered number is printed.

cout<<"Enter number : ";
cin>>n;
cout<<fact(n);

Updated on: 26-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements