C Program to print the Factorial of a Large Number in Modulo (10 ^ 9) + 7 (iterative 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>
int main()
{
    int n , M = 1000000007;
    long long fact = 1;
    printf("Enter the number : ");
    scanf("%d" , &n);
    for(int i = 2i <= ni = i + 1)
    {
        fact = (fact * i) % M// Using 2nd property
    }
    printf("\nThe Factorial of %d in Modulo
             (10 ^ 9) + 7 is : %lli\n\n" , n , fact);
    return 0;
}

Comments