**C Program to find factorial by recursion and iteration methods**

**C Program to find factorial by recursion and iteration methods**

Write a C Program to find factorial by recursion and iteration methods. Here’s a Simple Program to find factorial of a number using both recursive and iterative methods in C Programming Language.

This Program prompts user for entering any integer number, finds the factorial of input number and displays the output on screen.

**Factorial of a Number : :**

**Factorial of a Number : :**

A factorial of a number x is defined as the product of x and all positive integers below x. A factorial is product of all the number from 1 to the user specified number.

The factorial of a positive number n is given by ::

factorial of n (n!) = 1*2*3*4….n

The factorial of a negative number doesn’t exist. And the factorial of 0 is 1. You will learn to find the factorial of a number using recursion and iterative methods in this example.

**1. Using Recursion : :**

**1. Using Recursion : :**

We will use a recursive user defined function to perform the task. Here we have a function that calls itself in a recursive manner to find out the factorial of input number.

`fact( )`

**2. Using Iterative : :**

**2. Using Iterative : :**

To calculate the factorial in a for loop, it seems like all we would have to do is start from x and then multiply by all integer values below x, and just hold that value until we are done iterating.

Below is the source code for C Program to find factorial by recursion and iteration methods which is successfully compiled and run on Windows System to produce desired output as shown below :

**SOURCE CODE : :**

**SOURCE CODE : :**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
/* Program to find the factorial of a number by recursion and iteration method*/ #include<stdio.h> long int fact(int n); long int Ifact(int n); int main( ) { int num; printf("Enter a number : "); scanf("%d", &num); printf("\nUsing Recursion :: \n"); if(num<0) printf("No factorial for negative number\n"); else printf("Factorial of %d is %ld\n", num, fact(num) ); printf("\nUsing Iterative :: \n"); if(num<0) printf("No factorial for negative number\n"); else printf("Factorial of %d is %ld\n", num, Ifact(num) ); return 0; }/*End of main()*/ /*Recursive*/ long int fact(int n) { if(n == 0) return(1); return(n * fact(n-1)); }/*End of fact()*/ /*Iterative*/ long int Ifact(int n) { long fact=1; while(n>0) { fact = fact*n; n--; } return fact; }/*End of ifact()*/ |

**OUTPUT : :**

**OUTPUT : :**

1 2 3 4 5 6 7 |
Enter a number : 8 Using Recursion :: Factorial of 8 is 40320 Using Iterative :: Factorial of 8 is 40320 |

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may * Contact Us* through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.

**Thanks for reading the post….**

## Leave a Reply