# Write a C program to find the Factorial of a Number Using Recursion

January 04, 2021

To understand this, you should have basic knowledge in

• C user-defined and inbuilt functions
• C recursion

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

In this example, you will understand how to find the factorial of a positive number using recursion.

```    #include<stdio.h>
long int multiplyNums(int n);
int main() {
int number;
printf("Enter an integer number: ");
scanf("%d",&number);
printf("Factorial of the number %d = %ld", number, multiplyNums(number));
return 0;
}

long int multiplyNums(int num) {
if (num>=1)
return num*multiplyNums(num-1);
else
return 1;
}
```

OUTPUT

Enter an integer number: 5
Factorial of the number 5 = 120

Suppose the user entered 5.

Here we have a function multiplyNums() that calls itself in a recursive manner to find out the factorial of the input number. There is no recursive call if the value of num is less than 1. Finally, factorial is returned to the main function.

Then, 5 is passed to multiplyNums() from the same function (recursive call). In each recursive call, the value of argument num is decreased by 1.

0 0
January 04, 2021

Before going to the program, you should understand the mathematical logic to find the factorial of a number. The factorial function (symbol: !) says to multiply all positive numbers from our selected number down to 1.

The rule is, n! = n × (n−1)!

That means factorial of a number = factorial of a number * (factorial of a number - 1)

Example

5! = 5 × 4!
5! = 5 × 24 = 120

Note: 0! = 1

C program to find the factorial of a number

```    #include<stdio.h>
int findFact(int);
int main()
{
int num;
int factorial;
printf("Enter any positive integer number:");
scanf("%d",&num);
factorial =findFact(num);
//Displaying factorial of input number
printf("\nFactorial of the number %d is: %d",num, factorial);
return 0;
}
int findFact(int n)
{
if(n==0){
return(1);
}else{
return(n*findFact(n-1));
}
}
```

Output

Enter any positive integer number:5
Factorial of the number 5 is: 12

0 0
January 05, 2021

Below is a C program for finding factorial of a given number using recursion.  A straight definition of recursion is the property of function to call itself.

```    #include<stdio.h>
// function declaration
int fact(int);

int main()
{
int num, f;
printf("\nEnter a positive number: ");
scanf("%d", &num);
f= fact(num);
printf("Factorial of  %d is  %d", num, f);
return 0;
}

int fact(int number)
{
if(number==1 || number==0)
return 1;
else
return (number*fact(number-1));
}
```

OUTPUT

Enter a positive number: 5
Factorial of  5 is  120

0 0
January 05, 2021

C Program to calculate Factorial using recursion

```    #include<stdio.h> //include library stdio
long factorial(int num);//function declaration

long factorial(int num)
{
if(num == 0){
return 1;
}else{
return num * factorial(num -  1); // recursive call
}
}

int main(void)
{
int num;
printf("Enter a non-negative number: ");
scanf("%d", &num);
printf("The factorial of the number %d is %ld", num, factorial(num));
return 0;
}
```

OUTPUT

Enter a non-negative number: 5
The factorial of the number 5 is 120

0 0
January 05, 2021

Recursion is the property of the function call itself. To solve a problem using recursion in any programming language, you must express it in a recursive solution.

To calculate the number of permutations and combinations, and to compute the probability, we use factorials.

```    #include<stdio.h>
int findFact(int);

int findFact(int n)
{
if(n==1){
return 1;
}else{
return(n*findFact(n-1));// recursive function
}
}

int main()
{
int number,fact;
printf("Enter a +ve integer : ");
scanf("%d",&number);
fact=findFact(number);//call the function findFactorial for factorial
printf("The Factorial of %d is : %d",number,fact);
return 0;
}
```

OUTPUT

Enter a +ve integer: 5
The Factorial of 5 is : 120

0 0