Find the last two digits of Factorial of a given Number

Given an integer N, the task is to find the last two digits of factorial of a number.
Examples:

Input: N = 7
Output: 40
Explanation: 7! = 5040

Input: N = 11
Output: 00



Approach: We can observe that for N >= 10, the last two places of its factorial will contain 0’s only. Hence N! % 100 for any N >= 10 will always be 0. So we just calculate the factorial if N < 10 and extract the final two digits.

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to
// find last two digits
// factorial of a given number
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to print the
// last two digits of N!
void lastTwoDigits(long long N)
{
  
    // For N >= 10, N! % 100
    // will always be 0
    if (N >= 10) {
        cout << "00";
        return;
    }
  
    long long fac = 1;
    // Calculating N! % 100
    for (int i = 1; i <= N; i++)
        fac = (fac * i) % 100;
  
    cout << fac;
}
  
// Driver code
int main()
{
    int N = 7;
    lastTwoDigits(N);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to
// find last two digits
// factorial of a given number
import java.util.*;
class GFG{
  
// Function to print the
// last two digits of N!
static void lastTwoDigits(double N)
{
  
    // For N >= 10, N! % 100
    // will always be 0
    if (N >= 10) 
    {
        System.out.print("00");
        return;
    }
  
    double fac = 1;
      
    // Calculating N! % 100
    for (int i = 1; i <= N; i++)
        fac = (fac * i) % 100;
  
    System.out.print(fac);
}
  
// Driver code
public static void main(String args[])
{
    int N = 7;
    lastTwoDigits(N);
}
}
  
// This code is contributed by Code_Mech

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to
# find last two digits
# factorial of a given number
  
# Function to print the
# last two digits of N!
def lastTwoDigits(N):
  
    # For N >= 10, N! % 100
    # will always be 0
    if (N >= 10):
        print("00", end = "")
        return
  
    fac = 1
      
    # Calculating N! % 100
    for i in range(1, N + 1):
        fac = (fac * i) % 100
  
    print(fac)
  
# Driver code
if __name__ == '__main__':
    N = 7
    lastTwoDigits(N)
  
# This code is contributed by Mohit Kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to
// find last two digits
// factorial of a given number
using System;
class GFG{
  
// Function to print the
// last two digits of N!
static void lastTwoDigits(double N)
{
  
    // For N >= 10, N! % 100
    // will always be 0
    if (N >= 10) 
    {
        Console.Write("00");
        return;
    }
  
    double fac = 1;
      
    // Calculating N! % 100
    for (int i = 1; i <= N; i++)
        fac = (fac * i) % 100;
  
    Console.Write(fac);
}
  
// Driver code
public static void Main()
{
    int N = 7;
    lastTwoDigits(N);
}
}
  
// This code is contributed by Nidhi_biet

chevron_right


Output:

40

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.