C Factorial Program

C Factorial Program – You can find Factorial of a Number in C programming, in many ways. In this tutorial, we will go through following list of processes.

  1. Factorial using Recursion.
  2. Factorial using While Loop.
  3. Factorial using For Loop.

Factorial using Recursion

You can calculate factorial of a given number using recursion technique in C programming.

The steps to find factorial using recursion are:

  1. Read number from user.
  2. Write a function called factorial.
    1. If the argument is less than 2, the function should return 1.
    2. Else, the function should return the product of argument and factorial(argument – 1).
  3. Call factorial function with the number, read from user, as argument.

C Program

</>
Copy
#include <stdio.h>

int factorial(int n) {
    if (n<2) {
        return 1;
    } else {
        return n * factorial(n - 1);
    }
}

int main() {
    int n;
    printf("Enter a number : ");
    scanf("%d", &n);

    int result = factorial(n);
    printf("Factorial of %d is %d", n, result);
}

Output

C Factorial Program Output

Factorial using While Loop

By looking at the formula of factorial, we can use looping technique to find the result of factorial for a given number.

In the following program, we will use C While Loop to find factorial

The steps to find factorial using while loop are:

  1. Read number n from user. We shall find factorial for this number.
  2. Initialize two variables: result to store factorial, and i for loop control variable.
  3. Write while condition i <= n. We have to iterate the loop from i=1 to i=n.
    1. Compute result = result * i during each iteration.
    2. Increment i.
  4. After while loop execution, result contains the factorial of the number n.

C Program

</>
Copy
#include <stdio.h>

int main() {
    int n;
    printf("Enter a number : ");
    scanf("%d", &n);

    int result = 1;
    int i = 1;
    while (i <= n) {
        result *= i;
        i++;
    }

    printf("Factorial of %d is %d", n, result);
}

Output

C Factorial Program Output

Factorial using For Loop

Similar to that of While loop in the previous example, we can also use C For Loop to find the factorial of a number.

The steps to find factorial using for loop are:

  1. Read number n from user. We shall find factorial for this number.
  2. Initialize variable result to store factorial
  3. In for loop initialization, initialize i=1 for loop control variable. In the update section of for loop, increment i.
  4. Write for loop condition i <= n.
    1. Compute result = result * i during each iteration.
  5. After for loop execution, result contains the factorial of the number n.

C Program

</>
Copy
#include <stdio.h>

int main() {
    int n;
    printf("Enter a number : ");
    scanf("%d", &n);

    int result = 1;
    for (int i = 1; i <= n; i++) {
        result *= i;
    }

    printf("Factorial of %d is %d", n, result);
}

Output

C Factorial Program Output

Conclusion

In this C Tutorial, we learned to write C program to compute Factorial of a given number.