answersLogoWhite

0

// 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;

}

User Avatar

Wiki User

15y ago

What else can I help you with?

Related Questions

What is a recursive function?

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 &gt; 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.


How do you write a program to factorial in PHP?

To write a program that calculates the factorial of a number in PHP, you can use a recursive function or an iterative approach. Here’s a simple example using a loop: function factorial($n) { $result = 1; for ($i = 2; $i &lt;= $n; $i++) { $result *= $i; } return $result; } echo factorial(5); // Outputs: 120 This code defines a function that multiplies numbers from 2 up to the given number $n to compute the factorial.


Jntu 2-2 oops through java answers?

write a java program to find factorial using recursive and non recursive


Write a C-like program fragment that calculate the factorial function for argment 12 with do while loop?

#!/usr/bin/perl print factorial($ARGV[11]); sub factorial { my($num) = @_; if($num == 1) { return 1; # stop at 1, factorial doesn't multiply times zero } else { return $num * factorial($num - 1); # call factorial function recursively } }


A program for simple factorial in prolog?

In Prolog, a simple factorial program can be defined using recursion. Here's a basic implementation: factorial(0, 1). % Base case: factorial of 0 is 1 factorial(N, Result) :- N &gt; 0, N1 is N - 1, factorial(N1, Result1), Result is N * Result1. % Recursive case You can query the factorial of a number by calling factorial(N, Result). where N is the number you want to compute the factorial for.


Does javascript support recursive functions?

Yes, but a recursive function running for a long time would eventually cause your program to crash.


What is the difference between recursive and non recursive program?

A recursive system is one in which the output is dependent on one or more of its past outputs while a non recursive system is one in which the output is independent of any past outputs.e.g feedforward system having no feedback is a non recursive system.


How do you write a program that calculate factorial in javascript?

factorial number Var num= prompt(&quot;enter any number &quot;); Var i=1; Var fact=1; for(i=1;i


What is the benefit of using function.llustrate different ways of passing argument to function in c plus plus?

Functions are used to reduce the lines of code and the complexity of the code. For an instance let us suppose that you want to calculate the factorial of numbers at different times in a program. There are two ways to do this 1. Write a 4-5 line code every time you want to calculate factorial. 2. Write a function of 4-5 lines which calculates the factorial and call that function every time you need to calculate factorial by just writing a single line. In C++ you can pass the variable, address of the variable or a reference to the variable in a function


What is the program to calculate factorial value of given number using BC command in UNIX?

From the manpage of the bc(1) command: The following is the definition of the recursive factorial function. define f (x) { if (x &lt;= 1) return (1); return (f(x-1) * x); } So you could enter that definition of f(), and then call it, for example f(10)


What is analysis of recursive program?

1) Recursive algorithms 2) Basic Principle 3) Analysis


What is the merits and demerit of recursion in algorithm?

The advantages of recursion tend to revolve around the fact that there are quite a few algorithms which lend themselves to recursion (tree traversal, binary searches, quick sort, etc.) The disadvantages of recursion include: * finite number of recursive steps (limited heap space) * speed/efficiency (easier to increment a loop counter than call a function)