answersLogoWhite

0

Note: The recursive portions of this code can use a lot of stack space. You may need to tell your linker to allocate more space to prevent a stack overflow exception.

To calculate the factorial of a user given number, simply multiply all of the integers between 2 and that number, inclusive, together. There are two fundamental algorithms; iterative and recursive...

Iterative uses a loop of some sort to iterate through each multiplier and multiply that to the running product.

These examples were developed and checked on the Microsoft Windows SDK 7. Portable compilers might not like the 64-bit long long data type.

/* Microsoft 32-bit iterative */

unsigned long NFactLongIterative (unsigned long N) {

unsigned long result = N;

if (N < 2) return 1;

if (N 2) {

decimal_initialize (d, 2);

return;

}

while (N > 2) {

decimal_multiply (d, N);

N--;

}

return;

}

Main line code, showing full functionality

#include

... all of the above routines are inserted here

/* Example main line */

/* Generates all variations to show differences in results */

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

int N;

decimal Decimal = {2, NULL};

if (argc < 2) {

fprintf (stderr, "Usage: factorial N\n");

return 1;

}

N = atoi (argv[1]);

printf ("Long: %u! = %u\n", N, NFactLongIterative (N));

printf ("LongLong: %u! = %I64u\n", N, NFactLongLongIterative (N));

printf ("Recursive: %u! = %I64u\n", N, NFactLongLongRecursive (N));

printf ("Double: %u! = %.0f\n", N, NFactDouble (N));

/* note: arbitrary is exact - if the others don't match, arithmetic overflow occurred */

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

decimal_NFactIterative (&Decimal, N);

decimal_print_digits (&Decimal, TRUE);

return 0;

}

User Avatar

Wiki User

15y ago

What else can I help you with?

Related Questions

what is the flow chart and algorithm to find the factorial of a given number?

A flowchart to find the factorial of a given number typically includes the following steps: Start, read the input number, check if the number is less than 0 (return an error for negative numbers), initialize a result variable to 1, and then use a loop to multiply the result by each integer from 1 to the input number. The algorithm can be summarized as follows: if ( n ) is the input number, initialize ( \text{factorial} = 1 ); for ( i ) from 1 to ( n ), update ( \text{factorial} = \text{factorial} \times i ); finally, output the factorial.


What is the most efficient way to implement a factorial algorithm in a programming language?

The most efficient way to implement a factorial algorithm in a programming language is to use an iterative approach rather than a recursive one. This involves using a loop to multiply the numbers from 1 to the given input number to calculate the factorial. This method is more memory-efficient and faster than using recursion.


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).


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


Write a java program to find the factorial of a given number?

Here's a simple Java program to find the factorial of a given number using a recursive method: import java.util.Scanner; public class Factorial { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(&quot;Enter a number: &quot;); int number = scanner.nextInt(); System.out.println(&quot;Factorial of &quot; + number + &quot; is &quot; + factorial(number)); } static int factorial(int n) { return (n == 0) ? 1 : n * factorial(n - 1); } } This program prompts the user for a number and calculates its factorial recursively.


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


How do you determine whether a given number is a factorial?

To determine if a given number is a factorial, you can check if there exists a non-negative integer ( n ) such that ( n! ) equals that number. Start with ( n = 0 ) and compute factorials incrementally (0! = 1, 1! = 1, 2! = 2, etc.) until the computed factorial exceeds the given number. If you find a match during this process, then the number is a factorial; otherwise, it is not. Additionally, you can utilize the property that factorials grow very rapidly, which can help limit the number of calculations needed.


Find flow chart for factorial of given number?

i need a pic of cuson


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

kjhk


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

no answer....pls post


How many keywords are there in c?

answer:32 programme to print factorial of a given number in c languages


How many diagonals do hexagons have?

The number of diagonals in an n-sided polygon is given by nC2 - n (where n is the number of sides of the polygon) or in the expanded form: factorial (n) _______________________ {factorial (2) * factorial (n-2)} substituting (n = 6) for a hexagon we get the number of diagonals as 9. Similarly, substituting (n=5) for a pentagon we get the number of diagonals as 5.