-3

i want to calculate the factorial of the numbers greater than 30 , when i want to calculate factorial the result is 0 .

 #include <iostream>

 using namespace std;

 void fun1();

 int main()
 {
   fun1();
 }
 void fun1(){
  int x  ; 
  int  sum = 1 ;
  cout << "Enter your number:";
  cin >> x ;
  if ( x >= 1 ){
      for ( int i = x ; i > 1 ; i--){
          sum  *=   i ;
      }
  } 
  cout << "Factorial of " << x << " is :" << sum ;

 }
  • 1
    related/dupe: https://stackoverflow.com/questions/1966077/calculate-the-factorial-of-an-arbitrarily-large-number-showing-all-the-digits – NathanOliver May 24 '18 at 15:18
  • Possible duplicate of [Calculate the factorial of an arbitrarily large number, showing all the digits](https://stackoverflow.com/questions/1966077/calculate-the-factorial-of-an-arbitrarily-large-number-showing-all-the-digits) – dandan78 May 24 '18 at 15:32

1 Answers1

0

You can use larger data types (unsigned long long int if available).

You can cheat some digits off by checking whenever you have a 2 and 5 factors in the expression, removing them and remembering to output an extra 0 at the end:

6! = 6 * 5 * 4 * 3 * 2 = ( 6 * 4 * 3 ) * (5*2) = "72""0"

best of all you use a "large number" or "arbitrary precision" library

LSerni
  • 55,617
  • 10
  • 65
  • 107