How do you write a C program to calculate the factorial of given no?

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;

}
First answer by Logu122. Last edit by Dhivia M. Contributor trust: 0 [recommend contributor recommended]. Question popularity: 15 [recommend question].