answersLogoWhite

0


Best Answer

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

User Avatar

Wiki User

8y ago
This answer is:
User Avatar
User Avatar

PARTH SINGH PARIHAR

Lvl 1
2y ago
good but mention step no.
More answers
User Avatar

Wiki User

14y ago

public static final long factorial(final long n) {

if (n <= 1) {
return 1;

}

long _n = n;
for (int i = (int) (n - 1); i > 1; --i) {
_n *= i;

}

return _n;

}


we want an algorithm not code i mean not programing

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

You get the factorial by multiplying the number with every number before down to 1.

Factorial of 3 would be 3! = 3 * 2 * 1 = 6 or the factorial of 5 would be 5! = 5 * 4 * 3 * 2 * 1 = 120.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

The classic algorithm to find N Factorial is ...

int nfact (int n) {

if (n 2) {

decimal_initialize (d, 2);

return;

}

while (N > 2) {

decimal_multiply (d, N);

N--;

}

return;

}

/* Example main line */

int main (int argc, char *argv[]) {

int N;

decimal Decimal = {2, NULL};

if (argc < 2) {

printf ("Enter N (or use command line) : ");

scanf_s ("%d", &N);

} else {

N = atoi (argv[1]);

}

printf ("Arbitrary: %u! = ", N);

decimal_NFactIterative (&Decimal, N);

decimal_print_digits (&Decimal, true);

return 0;

}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

The problem with factorials is they get large quickly, and overflow the underlying hardware's capability of representation. The simple example...

unsigned long nfactorial (unsigned long N) return N 2) {

decimal_initialize (d, 2);

return;

}

while (N > 2) {

decimal_multiply (d, N);

N--;

}

return;

}

/* Example main line */

int main (int argc, char *argv[]) {

int N;

decimal Decimal = {2, NULL};

if (argc < 2) {

printf ("Enter N (or use command line) : ");

scanf_s ("%d", &N);

} else {

N = atoi (argv[1]);

}

printf ("Arbitrary: %u! = ", N);

decimal_NFactIterative (&Decimal, N);

decimal_print_digits (&Decimal, true);

return 0;

}

Now, this works well, though for larger numbers you will need to increase your stack size parameter at link time. It also starts to take time. One way to improve that is to store more than one digit in each node of the linked list decimal. You could even store an entire 32 bit word in each element, but the complexity of doing so for this example exceeds the value of the lesson. You would also need to implement a division operator, because you will need to convert to decimal for display purposes.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

For any positive integer, n, factorial (n) can be calculated as follows:

- if n<2, return 1.

- otherwise, return n * factorial (n-1).

The algorithm is recursive, where n<2 represents the end-point.

Thus for factorial (5) we find the following recursive steps:

factorial (5) = 5 * factorial (4)

factorial (4) = 4 * factorial (3)

factorial (3) = 3 * factorial (2)

factorial (2) = 2 * factorial (1)

factorial (1) = 1

We've now reached the end-point (1 is less than 2) and the results can now filter back up through the recursions:

factorial (2) = 2 * factorial (1) = 2 * 1 = 2

factorial (3) = 3 * factorial (2) = 3 * 2 = 6

factorial (4) = 4 * factorial (3) = 4 * 6 = 24

factorial (5) = 5 * factorial (4) = 5 * 24 = 120

Thus factorial (5) = 120.

We can also use a non-recursive algorithm. The factorial of both 0 and 1 is 1 thus we know that the return value will always be at least 1. As such, we can initialise the return value with 1. Then we begin iterations; while 1<n, multiply the return value by n and then subtract 1 from n. We can better represent this algorithm using pseudocode:

Function: factorial (n), where n is an integer such that 0<=n. Returns an integer, f.

Let f = 1

Repeat while 1<n

Let f = f * n

Let n = n - 1

End repeat

Return f

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Factorial of n:

fac = 1

while n > 1

fac = fac * n

n = n - 1

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Recursive Factorial function:

long int factorial(int n)

{

if (n<=1)

return(1);

else

n=n*factorial(n-1);

return(n);

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do algorithm to find the factorial of a number?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Write the Pseudocode to find the factorial of a number?

Pseudo code+factorial


Write an algorithm to print the factorial of given number and then draw the flowchart?

write an algorithm to print the factorial of a given number and then draw the flowchart. This looks like someones homework, defiantly someone looking for the easy way. When it comes to programming, the more you do the better you get. Experience counts (making your own mistakes and learning from the mistake).


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


Algorithm to find factorial using functions?

factorial using recursion style in c++ is unsigned int fact(unsigned int a) { if (a&lt;=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&lt;=n;i++) f*=i ; return f; }

Related questions

Write the Pseudocode to find the factorial of a number?

Pseudo code+factorial


Write an algorithm to print the factorial of given number and then draw the flowchart?

write an algorithm to print the factorial of a given number and then draw the flowchart. This looks like someones homework, defiantly someone looking for the easy way. When it comes to programming, the more you do the better you get. Experience counts (making your own mistakes and learning from the mistake).


An algorithm that generates all r-permutations of an n-element set?

P(n,r)=(n!)/(r!(n-r)!)This would give you the number of possible permutations.n factorial over r factorial times n minus r factorial


How can you figure out combinations in math?

If you have N things and want to find the number of combinations of R things at a time then the formula is [(Factorial N)] / [(Factorial R) x (Factorial {N-R})]


What algorithm allows to find the factorial?

Just multiply all the natural numbers from 1 to the number. For example, 7! = 7 x 6 x 5 x 4 x 3 x 2 x 1 = 5040.


Find flow chart for factorial of given number?

i need a pic of cuson


Find the number of distinguishable permutations of the letters in the word manatee?

Take the total number of letters factorial, then divide by the multiple letters factorial (a and e). 7! / (2!*2!) or 1260.


How do you find factorial of given number?

Factorials are the product of 1 and all the integers up to the given number. Simply put, 5 factorial or 5! = 5*4*3*2*1


By using call by reference find the factorial of a number?

chutia mc,bc bhosdika


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