answersLogoWhite

0


Best Answer

Use this function: long factorial(int N)

{

if (N == 0)

{

return 1;

}

else

{

return N*factorial(N-1);

}

}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

Factorial in C++ is the same as factorial in mathematics. For a given integer, N, the factorial, denoted N!, is the product of all integers in the closed range 1 to N, where 0! is 1. The problem with factorials is that the largest factorial you can store in a 64-bit integer is 20!. To cater for larger factorials you need a numeric library capable of handling larger integers, such as the GMP library.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

The factorial of a given number in C++ is the same as the factorial for a given number in mathematics. That is, the language is immaterial -- the algorithm remains the same.

The factorial of positive integer n is denoted n! and is the product of all positive integers less than or equal to n.

Thus the factorial of 5 is 5!, which 5 x 4 x 3 x 2 x 1 = 120.

Although the factorial of a given number is a recursive operation (and is often used to demonstrate recursive functions), an iterative approach uses less memory.

The basic algorithm is to successively multiply a variable initialised to 1 by all the integers from 2 to n. In C++ this can be implemented as follows:

int factorial(const int number)

{

int product = 1;

int multiplier = 1;

while (multiplier++<number)

product*=multiplier;

return product;

}

While the implementation is fairly simple, the main problem to overcome is that 12! is the largest factorial that will fit into a 32-bit integer. Even if we rewrite the function to work with 64-bit integers we're still limited to factorials up to 20!. Floating-point values give a bit more scope, but even 64-bit IEEE 754 floating-point can only cater for 170!. When you consider that the humble scientific calculator provided with Windows 7 and 8 can cater for factorials up to 3248!, we've clearly got a long way to go.

To cater for large factorials (and large numbers in general), you must first implement a large integer type that is capable of expanding to cater for overflows. In other words, the type should encapsulate a dynamic array and should (at the very least) implement the post-fix increment, compound multiply and less than operators. Thus if your type is called big_int, you can implement your factorial function as follows:

big_int factorial(const big_int number)

{

big_int product = 1;

big_int multiplier = product;

while (multiplier++<number) product*=multiplier;return product;

}

Your big_int type should ideally support all standard numeric operations as well conversion to and from integer strings for input/output purposes.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: C plus plus program to find factorial of given number using function overloading?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is a recursive function?

A recursive function is one that calls upon itself until a given result in the original call is met. Take a look at this example. Program Recursion; Uses crt; Var number:longint; Function Factorial(number:longint):longint; Begin if number &gt; 0 then factorial:=number*factorial(number-1) else factorial:=1; End; Begin clrscr; readln(number); writeln(factorial(number)); readln; End. Note how the function factorial calls itself.


What is a factorial function in Visual Basic?

' Iterative solution Function iterativeFactorial(ByVal n As Long) As Long Dim factorial As Long = 1 For i As Long = 1 To n factorial *= i Next Return factorial End Function ' Recursive solution Function recursiveFactorial(ByVal n As Long) As Long If n &lt;= 1 Then Return n End If Return n * recursiveFactorial(n - 1) End Function


Write a java script program to print first ten odd natural numbers in C?

Q.1 Write a program to print first ten odd natural numbers. Q.2 Write a program to input a number. Print their table. Q.3 Write a function to print a factorial value.


What are the similarities between constructor overloading and function overloading?

The only similarity is that both constructor and function overloads are distinguished by their signature -- the number and type of their arguments. Functions differ in that they also have a return type, which is also part of the signature, whereas constructors have no return type, not even void.


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;

Related questions

What is a recursive function?

A recursive function is one that calls upon itself until a given result in the original call is met. Take a look at this example. Program Recursion; Uses crt; Var number:longint; Function Factorial(number:longint):longint; Begin if number &gt; 0 then factorial:=number*factorial(number-1) else factorial:=1; End; Begin clrscr; readln(number); writeln(factorial(number)); readln; End. Note how the function factorial calls itself.


What is a factorial function in Visual Basic?

' Iterative solution Function iterativeFactorial(ByVal n As Long) As Long Dim factorial As Long = 1 For i As Long = 1 To n factorial *= i Next Return factorial End Function ' Recursive solution Function recursiveFactorial(ByVal n As Long) As Long If n &lt;= 1 Then Return n End If Return n * recursiveFactorial(n - 1) End Function


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

kjhk


What is the c plus plus program to calculate the area of a circle using function overloading?

Function overloading is used when you want to re-use the same function name with different argument types or a different number of arguments. Calculating the area of a circle isn't the sort of function that requires overloading since the only argument you need is the radius. double area_of_circle (const double radius) { const double pi=4*atan(1); return pi*radius*radius; }


What does an exclamation point symbol right next to a number do to the number?

An exclamation mark after a number is the symbol for the factorial function.


What is a Flow chart for finding factorial of a given number using recursion function?

no answer....pls post


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


How do you find factors of a number in Excel spreadsheet?

Use the FACT function. So to get the factorial of 5, you would enter:=FACT(5)Use the FACT function. So to get the factorial of 5, you would enter:=FACT(5)Use the FACT function. So to get the factorial of 5, you would enter:=FACT(5)Use the FACT function. So to get the factorial of 5, you would enter:=FACT(5)Use the FACT function. So to get the factorial of 5, you would enter:=FACT(5)Use the FACT function. So to get the factorial of 5, you would enter:=FACT(5)Use the FACT function. So to get the factorial of 5, you would enter:=FACT(5)Use the FACT function. So to get the factorial of 5, you would enter:=FACT(5)Use the FACT function. So to get the factorial of 5, you would enter:=FACT(5)Use the FACT function. So to get the factorial of 5, you would enter:=FACT(5)Use the FACT function. So to get the factorial of 5, you would enter:=FACT(5)


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


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); }


How do you find factorial of a given number using function in C.?

Use the following function: unsigned fact (const unsigned n) { return n&lt;2 ? 1 : n * fact (n-1); } Note that for a 32-bit unsigned integer, the largest factorial this function can accommodate is 12!