/*program for finding the factorial*/
void main()
{
int a,b=1;
clrscr();
printf("Please enter any number to find its factorial\n");
scanf("%d",&a);
while(a>1)
{
b=b*a;
a--;
}
printf("factorial is %d",b);
getch();
}
Pseudo code+factorial
by this program you can find the factorial: #include<iostream> using namespace std; main() { int n,x,f=1; cin>> n; x=0; while(x<n) { x++; f= f*x; } cout<<"factorial is"<<f<<"\n"; system("pause"); return 0; }
== == using recursions: unsigned int Factorial( unsigned int x) { if(x>0) { return ( x * Factorial(x-1)); } else { return(1); } } factorial: unsigned int Factorial( unsigned int x) { unsigned int u32fact = 1; if( x == 0) { return(1); } else { while(x>0) { u32fact = u32fact *x; x--; } } }
// Iterative solution unsigned long iterativeFactorial(const unsigned long n) { unsigned long i, factorial = 1; for(i = 1; i <= n; i++) { factorial *= i; } return factorial; } // Recursive solution unsigned long recursiveFactorial(const unsigned long n) { if(n <= 1) { return n; } return n * recursiveFactorial(n - 1); } // Sample calls int main() { unsigned long n; printf("Enter a number to find its factorial: "); scanf("%u",&n); printf("Iterative factorial: %u\n", iterativeFactorial(n)); printf("Recursive factorial: %u\n", recursiveFactorial(n)); return 0; }
The factorial f(n) = n * (n-1) * (n-2) * .. 1. For example factorial 5 (written as 5!) = 5 x 4 x 3 x 2 x 1 = 120. The function below returns the factorial of the parameter n. int factorial( int n) { if (n==1) return 1 else return n* factorial( n-1) ; }
write a java program to find factorial using recursive and non recursive
recursion is always slower than iteration
i love u darling
Pseudo code+factorial
int main (void) { puts ("18826771768889260997437677024916008575954036487149242588759823150835\ 31563316135988668829328894959231336464054459300577406301619193413805\ 97818883457558547055524326375565007131770880000000000000000000000000\ 000000"); return 0; }
Note: You may need a larger data type, factorials become very big very quickly and may cause an overflow long factorial(int x) { if(x == 1) return 1; . return((long) factorial(x-1) * x);
this is a code for calculating it recursivelly: float Factorial (float n) { if (n<=1) return 1.0; else return n* Factorial(n-1); }
by this program you can find the factorial: #include<iostream> using namespace std; main() { int n,x,f=1; cin>> n; x=0; while(x<n) { x++; f= f*x; } cout<<"factorial is"<<f<<"\n"; system("pause"); return 0; }
I suggest to use a for loop, more or less like this (assuming the parameter is "n"): product = 1; for (int i = 1; i <= n; i++) { product *= i; }
#file.sh#run as sh file.shecho "Enter the no."read ni=nr=1while [ $i -ge 1 ]dor=`expr $r \* $i`i=`expr $i-1`doneecho Factorial is $rIf the ans helps you,plz increase the trust point.
== == using recursions: unsigned int Factorial( unsigned int x) { if(x>0) { return ( x * Factorial(x-1)); } else { return(1); } } factorial: unsigned int Factorial( unsigned int x) { unsigned int u32fact = 1; if( x == 0) { return(1); } else { while(x>0) { u32fact = u32fact *x; x--; } } }
// Iterative solution unsigned long iterativeFactorial(const unsigned long n) { unsigned long i, factorial = 1; for(i = 1; i <= n; i++) { factorial *= i; } return factorial; } // Recursive solution unsigned long recursiveFactorial(const unsigned long n) { if(n <= 1) { return n; } return n * recursiveFactorial(n - 1); } // Sample calls int main() { unsigned long n; printf("Enter a number to find its factorial: "); scanf("%u",&n); printf("Iterative factorial: %u\n", iterativeFactorial(n)); printf("Recursive factorial: %u\n", recursiveFactorial(n)); return 0; }