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! = 5040Input: 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 |
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
Recommended Posts:
- Find sum of digits in factorial of a number
- Smallest number with at least n digits in factorial
- Number of digits in N factorial to the power N
- Find smallest number with given number of digits and sum of digits under given constraints
- Find the length of factorial of a number in any given base
- Find the first natural number whose factorial is divisible by x
- Find the last digit when factorial of A divides factorial of B
- Find the average of k digits from the beginning and l digits from the end of the given number
- Find maximum power of a number that divides a factorial
- Find the Largest number with given number of digits and sum of digits
- Count digits in a factorial | Set 1
- Count digits in a factorial | Set 2
- Find smallest possible Number from a given large Number with same count of digits
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Find count of digits in a number that divide the number
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.

