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 codeint main(){ int N = 7; lastTwoDigits(N);} |
Java
// Java implementation to// find last two digits// factorial of a given numberimport 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 codepublic 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 codeif __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 numberusing 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 codepublic 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> |
40
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


