C Program to Find Factorial of a Number.

In this article, we are going to learn how to find the factorial of a number using C programming. Before moving to the program part, we first need to get a basic understanding of Factorial.

Factorial of a Number Formula

What is Factorial?

Factorial is a mathematical operation used to calculate the product of all positive integers from 1 up to a given number. It is denoted by the symbol !. For example, the factorial of 5 is represented as 5! and calculated as follows:

Factorial of 5 is:
5! = 5 × 4 × 3 × 2 × 1 = 120

Factorial of 4 is:
4! = 4 × 3 × 2 × 1 = 24

Factorials are commonly used in mathematics and various applications, such as combinations and permutations. They grow rapidly as the input number increases, and the factorial of 0 is defined as 1. Factorials are integral in solving problems related to counting arrangements, possibilities, and probabilities.

Find the Factorial of a Number.

Approach 1: Using the Iterative Method.

In this approach, we calculate the factorial of a number using a loop to multiply the numbers from 1 to the given number.

Step-by-step algorithm:

Step 1: Input the number for which you want to find the factorial.
Step 2: Initialize a variable "factorial" to 1.
Step 3: Use a loop to multiply "factorial" by numbers from 1 to the input number.
Step 4: Repeat the multiplication until the loop reaches the input number.
Step 5: The final value of "factorial" will be the factorial of the input number.

C Program to find the Factorial of a number using for loop.
//C program to find factorial using for loop
#include <stdio.h>

unsigned int findFactorialIterative(int num) {
    unsigned int factorial = 1;

    for (int i = 1; i <= num; i++) {
        factorial *= i;
    }

    return factorial;
}

int main() {
    int number;

    printf("Enter a positive integer: ");
    scanf("%d", &number);

    if (number < 0) {
        printf("Factorial is not defined for negative numbers.\n");
    } else {
        printf("Factorial of %d is %d.\n", number, findFactorialIterative(number));
    }

    return 0;
}
Output:
Enter a positive integer: 5
Factorial of 5 is 120.

Time Complexity: O(n) where n is the input number.
Space Complexity: O(1) as no extra space is required.

Approach 2: Using Recursive Method.

In this approach, we are using a recursive method to find the factorial of a number that uses a function that calls itself with smaller subproblems until it reaches the base case.

Step-by-step algorithm:

Step 1: Input the number for which you want to find the factorial.
Step 2: Create a recursive function to calculate the factorial.
Step 3: The base case is when the input number is 0 or 1, return 1 (factorial of 0 and 1 is 1).
Step 4: For other numbers, call the recursive function with (num - 1) and multiply it by "num."
Step 5: The final result obtained from the recursive calls will be the factorial of the input number.

C Program to find the Factorial of a num using Recursion.
//C implementation to find factorial of a number using recursion
#include <stdio.h>

unsigned int findFactorialRecursive(int num) {
    //base case
    if (num == 0 || num == 1) {
        return 1;
    } else {
        return num * findFactorialRecursive(num - 1);
    }
}

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

    if (number < 0) {
        printf("Factorial is not defined for negative numbers.\n");
    } else {
        printf("Factorial of %d is %d.\n", number, findFactorialRecursive(number));
    }

    return 0;
}
Output:
Enter a positive integer: 8
Factorial of 8 is 40320.

Time Complexity: The time complexity of the recursive method is also O(n), where "n" is the input number.
Space Complexity: The space complexity is O(n) as stack space is used to perform recursion.

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS