What would you like to do?

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

already exists.

Would you like to merge this question into it?

already exists as an alternate of this question.

Would you like to make it the primary and merge this question into it?

exists and is an alternate of .

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

}
17 people found this useful
Thanks for the feedback!

What is a program in c to calculate factorial of number?

First of all we will define what factorial is and how to it is calculated. Factional is non negative integer. Notation would be n! It is calculated by multiplying all intege

How do you write a C program that calculates parking charges for a given day?

#include   minfee = 2;   int main()  {  int car1,car2,car3,hour,hour2,hour3,charge1,charge2,charge3,  totalhour,totalcharge,minfee; 

Write a C program to calculate factorial of given positive integer no?

include 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("%

How can you write a program in c language to calculate factorial of 100 or above that?

  The largest factorial in a 32 bit unsigned integer is 12!, which is 479,001,600. The largest factorical in a 64 bit unsigned integer is 20!, which is 2,432,902,008,176,6

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

This program will compute N Factorial for any N, and does not suffer from overflow, because it uses a bin technique and handles each digit individually. For large values of N,

Write a C program to calculate a factorial?

include   #include   void main()   {   int a,b,c=1;   clrscr();   {   printf("enter the number b=");   scanf("%d",&b);   }   for(a=b;a>

How do you write a c program to calculate factorial using recursion?

unsigned long nfact(int n) if (n2) return n else return n*nfact(n-1); For 32-bit integers, this program will fail at N13, due to overflow. For 64-bit integers, it will fail at

How do you write a C program to calculate the factorial within recursive function?

Note: This fails at N=20 due to truncation error #include #define FALSE (0) #define TRUE (1) } /* Microsoft 64-bit recursive */ unsigned long long NFactLongLongRecursive (

How do you write a C program to find factorial of given integer using function?

There are several methods, some recursive, and iterative. Most suffer from lack of precision, because factorials get large fast. Here is a routine that compares various method

Write a program in c plus plus to compute the factorial of a given number?

The factorial of a real number, n, is the product of all positive real numbers less than or equal to n, denoted as n!. Thus, 5! = 5*4*3*2*1 = 120. Computing factorials is a n