answersLogoWhite

0


Best Answer

The factorial of 0 is indeed 1, but that's not really a problem. The problem is that the factorial of 20 is 2,432,902,008,176,640,000 and that's the largest factorial that will fit in a 64-bit integer. With only 21 factorials (including 0) to play with, a simple lookup table would be a lot quicker than calculating each of them separately.

unsigned __int64 factorial(unsigned num)

{

switch (num)

{

case (0):

case (1): return 1;

case (2): return 2;

case (3): return 6;

case (4): return 24;

case (5): return 120;

//....

case (20): return 2432902008176640000;

}

return 0; // indicates error!

}

To calculate them individually, use the following function:

unsigned __int64 factorial (unsigned num)

{

if (20<num) return 0; // indicates error!

unsigned __int64 result= 1;

while (1<num) result *= num--;

return result;

}

To accommodate factorials greater than 20 you could use a 64-bit double-precision float, however the results will be an approximation rather than precise and the risk of overflowing still exists. The best solution is to employ a user-defined, dynamically-sized type specifically designed to cater for large integrals (although they'd no longer be integral, of course). There are many libraries available to cater for this but, personally, I use the GMP library as it's one of the fastest available.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a c plus plus program that would accept a positive number and then output its factorial recall that the factorial of 0 is 1?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Write a program in java for factorial?

// Iterative solution public static final long iterativeFactorial(final long n) { long factorial = 1; for (long i = 1; i &lt;= n; i++) { factorial *= i; } return factorial; } // Recursive solution public static final long recursiveFactorial(final long n) { if (n &lt;= 1) { return n; } return n * recursiveFactorial(n - 1); } // Arbitrary length solution - may take a while, but works on any positive number. public static final BigInteger factorial(final BigInteger n) { BigInteger factorial = BigInteger.ONE; for (BigInteger i = BigInteger.ONE; i.compareTo(n) &lt;= 0; i = i.add(BigInteger.ONE)) { factorial = factorial.multiply(i); } return factorial; }


Write a recursive procedure to compute the factorial of a number?

#include &lt;iostream&gt; using namespace std; int main() { int i, number=0, factorial=1; // User input must be an integer number between 1 and 10 while(number&lt;1 number&gt;10) { cout &lt;&lt; "Enter integer number (1-10) = "; cin &gt;&gt; number; } // Calculate the factorial with a FOR loop for(i=1; i&lt;=number; i++) { factorial = factorial*i; } // Output result cout &lt;&lt; "Factorial = " &lt;&lt; factorial &lt;&lt; endl;


Programming to calculate a factorial number?

double factorial(double N){double total = 1;while (N > 1){total *= N;N--;}return total; // We are returning the value in variable title total//return factorial;}int main(){double myNumber = 0;cout > myNumber;cout


Program to find factorial of a number using inheritance?

/*71.PROGRAM TO FIND FACTORIAL OF A NUMBER USING RECURSION*/ #include&lt;stdio.h&gt; #include&lt;conio.h&gt; int fact(int); void main() { int n,f; clrscr(); printf("Enter number whose factorial is to be calculated: "); scanf("%d",&amp;n); if(n&gt;0) { f=fact(n); printf("factorial of %d is %d",n,f); } else printf("Factorial of numbers less than 1 does not exist"); getch(); } int fact(int n) { int facto=1; if(n&gt;1) facto=n*fact(n-1); else return 1; return(facto); }


Write the Pseudocode to find the factorial of a number?

Pseudo code+factorial

Related questions

What is the factorial of any number?

a factorial number is a number multiplied by all the positive integers i.e. 4!=1x2x3x4=24 pi!=0.14x1.14x2.14x3.14 0!=1


How do you do a factorial?

You first look at the number that is before the !(factorial sign). Then you times all positive integers (which means it doesn't include 0), including the number itself. The answer is the factorial of the original number beside the ! sign. EX.:4!=1x2x3x4=24


Program for finding the factorial of the two given number using constructor?

kjhk


Which number is a factor of all positive numbers?

1 is a factor of all positive numbers.


What is a factorial of a number?

A factorial of a positive integer n, is the product of all positive integers less than or equal to n. For example the factorial of 5 is: 5! = 5 x 4 x 3 x 2 x 1 = 120 0! is a special case that is explicitly defined to be 1. A factorial is denoted by n! (5! for this example)


Write a program using while loop?

//program to find the factorial value f any number using while loop #include&lt;stdio.h&gt; void main() { int i,n,fact=1; printf("Enter the number\n"); scanf("%d",&amp;n); i=n; while (i&gt;=1) { fact=fact*i; i--; } printf("The factorial value=%d",fact); } the above is a program for calculating tha factorial value of any number which is entered by the user


What is a program in c to calculate factorial of number?

First of all we will define what factorial is and how to it is calculated.Factional is non negative integer. Notation would be n! It is calculated by multiplying all integers from 1 to n;For example:5! = 1 x 2 x 3 x 4 x 5 = 120.Note: 0! = 1Small C program that illustrates how factorial might be counted:#include int factorial(int num);int main() {int num;printf("Enter number: ");scanf("%d", &num);printf("Factorial: %d\n", factorial(num));return 0;}int factorial(int num) {if (num == 0) {return 1;}return num * factorial(num - 1);}Testing:Enter number: 5Factorial: 120Enter number: 0Factorial: 1


Write a program in java for factorial?

// Iterative solution public static final long iterativeFactorial(final long n) { long factorial = 1; for (long i = 1; i &lt;= n; i++) { factorial *= i; } return factorial; } // Recursive solution public static final long recursiveFactorial(final long n) { if (n &lt;= 1) { return n; } return n * recursiveFactorial(n - 1); } // Arbitrary length solution - may take a while, but works on any positive number. public static final BigInteger factorial(final BigInteger n) { BigInteger factorial = BigInteger.ONE; for (BigInteger i = BigInteger.ONE; i.compareTo(n) &lt;= 0; i = i.add(BigInteger.ONE)) { factorial = factorial.multiply(i); } return factorial; }


7 Write a C program to compute the factorial of a number using for loop?

int factorial(int n) { int i; int f=1; for(i=2;i&lt;=n;++i) f*=i; return f; }


Program to find the factorial of a number using recursion?

/*program to find the factorial of a given number*/ #include&lt;stdio.h&gt; #include&lt;conio.h&gt; int fact(int); void main() { int n,c; printf("\n enter the number for which you want to find the factorial"); scanf("%d",&amp;n); c=fact(n); printf("\n the factorial of the number %d is %d",n,fact); getch(); } int fact(int n) { int k; if(n==0) return(1); else k=n*fact(n-1); return(k); }


Write a recursive procedure to compute the factorial of a number?

#include &lt;iostream&gt; using namespace std; int main() { int i, number=0, factorial=1; // User input must be an integer number between 1 and 10 while(number&lt;1 number&gt;10) { cout &lt;&lt; "Enter integer number (1-10) = "; cin &gt;&gt; number; } // Calculate the factorial with a FOR loop for(i=1; i&lt;=number; i++) { factorial = factorial*i; } // Output result cout &lt;&lt; "Factorial = " &lt;&lt; factorial &lt;&lt; endl;


Programming to calculate a factorial number?

double factorial(double N){double total = 1;while (N > 1){total *= N;N--;}return total; // We are returning the value in variable title total//return factorial;}int main(){double myNumber = 0;cout > myNumber;cout