Use this function: long factorial(int N)
{
if (N == 0)
{
return 1;
}
else
{
return N*factorial(N-1);
}
}
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.
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.
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 > 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.
' 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 <= 1 Then Return n End If Return n * recursiveFactorial(n - 1) End Function
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.
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.
#include <iostream> using namespace std; int main() { int i, number=0, factorial=1; // User input must be an integer number between 1 and 10 while(number<1 number>10) { cout << "Enter integer number (1-10) = "; cin >> number; } // Calculate the factorial with a FOR loop for(i=1; i<=number; i++) { factorial = factorial*i; } // Output result cout << "Factorial = " << factorial << endl;
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 > 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.
' 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 <= 1 Then Return n End If Return n * recursiveFactorial(n - 1) End Function
kjhk
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; }
An exclamation mark after a number is the symbol for the factorial function.
no answer....pls post
//program to find the factorial value f any number using while loop #include<stdio.h> void main() { int i,n,fact=1; printf("Enter the number\n"); scanf("%d",&n); i=n; while (i>=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
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)
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
int factorial(int n) { int i; int f=1; for(i=2;i<=n;++i) f*=i; return f; }
/*program to find the factorial of a given number*/ #include<stdio.h> #include<conio.h> int fact(int); void main() { int n,c; printf("\n enter the number for which you want to find the factorial"); scanf("%d",&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); }
Use the following function: unsigned fact (const unsigned n) { return n<2 ? 1 : n * fact (n-1); } Note that for a 32-bit unsigned integer, the largest factorial this function can accommodate is 12!