C Program to print the Factorial of a Large Number in Modulo (10 ^ 9) + 7 (Recursive approach)


 
// Program by Akash Tripathi (@proakash256) 
 
// The remainder obtained after the
// division (%) operation on two operands
// is known as Modulo Operation.

// The reason for taking mod is to prevent
// integer overflows.

// The number should be large enough to fit
// in the largest integer data type and it
// should be prime so as to compute 
// the modulo inverse.
// (10 ^ 9) + 7 fulfills both the criteria.

// Some properties of modulo are :

// 1. (a + b) % M = ((a % M) + (b % M)) % M;
// 2. (a * b) % M = ((a % M) * (b % M)) % M;
// 3. (a - b) % M = ((a % M) - (b % M) + M) % M;
// 4. (a / b) % M = ((a % M) * ((b ^ -1) % M)) % M;

#include <stdio.h>
long long factorial(int nint M)
{
    if(n == 1)
        return 1;
    return (n % M) * (factorial((n - 1) , M)) % M;
}
int main()
{
    int nM = 1000000007;
    long long fact = 1;
    printf("Enter the number : ");
    scanf("%d", &n);
    if (n == 0)
    {
        printf("\nThe Factorial of %d in Modulo
(10 ^ 9) + 7 is : %d\n\n"n1);
    }
    else
    {
        fact = factorial(n , M);
        printf("\nThe Factorial of %d in Modulo
                (10 ^ 9) + 7 is : %lli\n\n"nfact);
    }
    return 0;
}

Comments