Skip to content
Related Articles
Open in App
Not now

Related Articles

Find the last two digits of Factorial of a given Number

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 31 Aug, 2022
Improve Article
Save Article

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++




// 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);
}

Java




// 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

Python3




# 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

C#




// 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

Javascript




<script>
 
// JavaScript implementation to
// find last two digits of
// factorial of a given number
 
// Function to print the
// last two digits of N!
function lastTwoDigits(N)
{
 
    // For N >= 10, N! % 100
    // will always be 0
    if (N >= 10) {
        cout << "00";
        return;
    }
 
    let fac = 1;
    // Calculating N! % 100
    for (let i = 1; i <= N; i++)
        fac = (fac * i) % 100;
 
    document.write(fac);
}
 
// Driver code
    let N = 7;
    lastTwoDigits(N);
 
 
// This code is contributed by Surbhi Tyagi.
 
</script>

Output: 

40

 

Time complexity: O(n) since using a for loop

Auxiliary space: O(1) because it is using constant space


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!