Factorial Program in C Using Recursion

The factorial of any positive integer or non-negative number x is equivalent to the multiplication of every integer that is smaller than this non-negative integer x.

In this article, we will take a look at the Fibonacci Series Using Recursion in C and its uses according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to learn more.

Table of Contents

What is a Factorial Program in C?

The factorial of a positive number “n” refers to the product of all the descending integers before it (smaller than the number x). The factorial of a number is denoted by the symbol “!”. Thus, the factorial of a number will be “number!”. For instance, we will write the factorial of the non-negative integer x as x!.

Here,

x! = x * (x-1) * (x-2) * (x-3) * (x-4) … *.

Let us look at an example,

6! = 6x5x4x3x2x1 = 720

We can also write it as:

6! = 6 * 5!

We will pronounce 6! as 6 factorial. As a matter of fact, it is also known as 6 shriek or 6 bang. We normally utilise factorials in Permutations and Combinations (in Mathematics).

Let us look at a few more examples.

3! = 3x2x1 = 6

5! = 5x4x3x2x1 = 120

7! = 7x6x5x4x3x2x1 = 5040

Now let us see how we will write a factorial for a C program.

How to Write Factorial in C?

We can generate the factorial of a number as an output of a C program in various ways, such as:

  • Factorial program in C using For Loop
  • Factorial program in C using Ternary Operator
  • Factorial program in C using While Loop
  • Factorial program in C using Recursion

In this article, we are going to learn how we write a factorial program in C using recursion. But before we do that, let us understand the basic algorithm of writing factorials.

C Program Algorithm for the Factorials

Here is the basic algorithm followed in the C program for finding the factorial of any given number in the input:

  • Start the program;
  • The user will be asked about the integer for which they want the factorial;
  • The program reads the integer and then assigns its value to a variable in the code;
  • Every digit- starting from the given value, descending down to the integer 1- will be multiplied together. A final value will be, thus, generated.
  • The final value obtained after the multiplication of the integers refers to the factorial of the number.
  • The program will end.

Pseudocode for Factorial in C Programs

We can use the algorithm mentioned above to generate pseudocode that would generate the factorial of a number in a C program.

The code goes like this:

procedure_of_program

factorial(number)

until number=1

factorial = factorial*(num-1)

Print factorial // the factorial will be generally denoted as fact

end_of_procedure

Now that we all understand the pseudocode and the basic algorithm involved in generating a factorial using a C program, let us see how we use it in the recursive method. Keep in mind that we use the same pseudocode and algorithms in all the other methods as well.

Factorial Program in C Using Recursion

Here, we will see how we can use the recursive functions to write the factorial in a C program. Remember that the recursive function will continually keep calling itself unless it reaches the value 0.

#include <stdio.h>

int main(){

int s = 7;

printf(“If we calculate the factorial of the given variable s, the output generated would be equal to: %d”, fact(s));

return 0;

}

// The recursive function used in the to find factorial of s

int fact(int t){

if (t == 0)

return 1;

return t * fact(t – 1);

}

The output generated from the code mentioned above would be:

If we calculate the factorial of the given variable s, the output generated would be equal to: 5040

Examples

Let us look at a few more examples,

#include<stdio.h>

long fact(int a)

{

if (a == 0)

return 1;

else

return(a * fact(a-1));

}

void main()

{

int num;

long fact;

printf(“Please enter a number for factorial : “);

scanf(“%d”, &num);

fact = fact(num);

printf(“The factorial of the number %d is equal to : %ld\n”, num, fact);

return 0;

}

The output of the code mentioned above would be like this:

Please enter a number for factorial : 4

The factorial of the number %d is equal to : 24

#include<stdio.h>

// writing a recursive function to find the factorial of a variable

int fact (int m)

{

if(m!=0)

return m*fact(m-1); // the general case of numbers

else

return 1; // the base case in the end

} int main()

{

int number, result;

printf(“Please enter a non-negative number or integer : “);

scanf(“%d”,&num);

result= fact(num); // the function call in the code

printf(“The result of the factorial would be equal to : %d\n”,result);

return 0;

}

The output of the code mentioned above would be like this:

Please enter a non-negative number or integer : 8

The result of the factorial would be equal to : 40320

Practice Problems on Factorial Program in C Using Recursion

Take a look at the following program, and find the output for different sets of inputs:

#include<stdio.h>

long fact(int a)

{

if (a == 0)

return 1;

else

return(a * fact(a-1));

}

void main()

{

int num;

long fact;

printf(“Please enter a number for factorial : “);

scanf(“%d”, &num);

fact = fact(num);

printf(“The factorial of the number %d is equal to : %ld\n”, num, fact);

return 0;

}

The output of the following inputs would be:

1. Input: 5

Answer: Please enter a number for factorial : 5

The factorial of the number %d is equal to : 120

2. Input: 11

Answer: Please enter a number for factorial : 11

The factorial of the number %d is equal to : 39916800

3. Input: 9

Answer: Please enter a number for factorial : 9

The factorial of the number %d is equal to : 362880

4. Input: 7

Answer: Please enter a number for factorial : 7

The factorial of the number %d is equal to : 39916800


Frequently Asked Questions

Is there a factorial of the number zero (0)?

Yes, the factorial of 0 is 1. Let us see how.
As we all know,
5! = 5x4x3x2x1 = 120
We can then write this as follows:
5! = 5 * 4!
Similarly, we can write
4! = 4 * 3!
i.e., 3! = 3 * 2!
i.e., 2! = 2 * 1!
i.e., 1! = 1 * 0!
i.e., 0! = 1

What are the other names for a factorial value?

We will pronounce 6! as 6 factorial. Along with this, it is also known as 6 shriek or 6 bang.

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility CriteriaGATE 2023GATE Admit CardGATE Syllabus for CSE (Computer Science Engineering)GATE CSE NotesGATE CSE Question Paper, and more.

Also Explore,

Leave a Comment

Your Mobile number and Email id will not be published.

*

*