answersLogoWhite

0


Best Answer

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

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

#include <stdio.h>

long double fact (long double n) {

return (n == 1) ? 1 : n * fact(n-1);

}

void main()

{

int n;

printf("Enter a number:\n>");

scanf("%d",&n);

printf("%d! = %Lf\n", fact(n));

//long double can hold much bigger numbers than say, int or double.

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Note: This fails at N=20 due to truncation error

#include <stdio.h>

#define FALSE (0)

#define TRUE (1)

}

/* Microsoft 64-bit recursive */

unsigned long long NFactLongLongRecursive (unsigned long long N) {

if (N < 2) return 1;

if (N == 2) return 2;

return N * NFactLongLongRecursive (N - 1);

/* Example main line */

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

int N;

if (argc < 2) {

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

return 1;

}

N = atoi (argv[1]);

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

return 0;

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a C program to calculate the factorial within recursive function?
Write your answer...
Submit
Still have questions?
magnify glass
imp
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.


Jntu 2-2 oops through java answers?

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


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

function factorial(n) { var x=1; while(n&gt;1) x*=(n--); return x; }


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


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.


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


What is analysis of recursive program?

1) Recursive algorithms 2) Basic Principle 3) Analysis


Write a program in java for factorial?

// Iterative solution public static final long iterativeFactorial(final long n) { long factorial = 1; for (long i = 1; i &lt;= n; i++) { factorial *= i; } return factorial; } // Recursive solution public static final long recursiveFactorial(final long n) { if (n &lt;= 1) { return n; } return n * recursiveFactorial(n - 1); } // Arbitrary length solution - may take a while, but works on any positive number. public static final BigInteger factorial(final BigInteger n) { BigInteger factorial = BigInteger.ONE; for (BigInteger i = BigInteger.ONE; i.compareTo(n) &lt;= 0; i = i.add(BigInteger.ONE)) { factorial = factorial.multiply(i); } return factorial; }


Implement a program to find out the reverse of an integer using recursive functions?

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