factorial using recursion style in c++ is
unsigned int fact(unsigned int a)
{
if (a<=1)
return 1;
else
{
f*=fact(a-1);
return a;
}
}
when using looping structure factorial is
unsigned int fact (unsigned int n)
{
unsigned int i,f=1;
for(i=1;i<=n;i++)
f*=i ;
return f;
}
Pseudo code+factorial
If you really wanted to do this, you could simulate multiplication with repeated addition.
Factorial (n) = n * Factorial (n-1) for all positive values n given Factorial (1) = Factorial (0) = 1. Pseudo-code: Function: factorial, f Argument: positive number, n IF n<=1 THEN RETURN 1 ELSE RETURN n * f(n-1) END IF
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; }
/*71.PROGRAM TO FIND FACTORIAL OF A NUMBER USING RECURSION*/ #include<stdio.h> #include<conio.h> int fact(int); void main() { int n,f; clrscr(); printf("Enter number whose factorial is to be calculated: "); scanf("%d",&n); if(n>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>1) facto=n*fact(n-1); else return 1; return(facto); }
Using the extended Euclidean algorithm, find the multiplicative inverse of a) 1234 mod 4321
chutia mc,bc bhosdika
Using the Euclidean algorithm
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);
Yes. But why?
Pseudo code+factorial
#include #include using std::cin;using std::cout;using std::endl;using std::tolower;long factorial(const int& N);int main(){int N = 0; //factorial of Nchar command = 'n';do{cout > N;cout
If you really wanted to do this, you could simulate multiplication with repeated addition.
Kat
Write an algorithm to find the root of quadratic equation
Factorial (n) = n * Factorial (n-1) for all positive values n given Factorial (1) = Factorial (0) = 1. Pseudo-code: Function: factorial, f Argument: positive number, n IF n<=1 THEN RETURN 1 ELSE RETURN n * f(n-1) END IF
write a java program to find factorial using recursive and non recursive