Find last digit in factorial - GeeksforGeeks

26 Feb, 2021
Given a number n, we need to find the last digit in factorial n.
Input : n = 4Output : 44! = 4 * 3 * 2 * 1. =  24.  Last digit of 24 is 4.
Input : n = 5Output : 55! = 5*4 * 3 * 2 * 1. =  120.  Last digit of 120 is 0.
A Naive Solution is to first compute fact = n!, then return the last digit of the result by doing fact % 10.  This solution is inefficient and causes integer overflow for even slightly large value of n.
An Efficient Solution is based on the observation that all factorials after 5 have 0 as last digit.
1! = 12! = 23! = 64! = 245! = 1206! = 720..............
C++
Java
Python3
C#
Javascript
// C++ program to find last digit in// factorial n.#include <iostream>using namespace std; int lastDigitFactorial(unsigned int n){   // Explicitly handle all numbers   // less than or equal to 4   if (n == 0) return 1;   else if (n <= 2) return n;   else if (n == 3) return 6;   else if (n == 4) return 4;      // For all numbers greater than 4   // the last digit is 0   else return 0;} int main() {     cout<<lastDigitFactorial(6);    return 0;}
// Java program to find last // digit in factorial n.import java.io.*;import java.util.*; class GFG {     static int lastDigitFactorial(int n){         // Explicitly handle all numbers    // less than or equal to 4    if (n == 0) return 1;    else if (n <= 2) return n;    else if (n == 3) return 6;    else if (n == 4) return 4;         // For all numbers greater than    // 4 the last digit is 0    else return 0;} // Driver codepublic static void main(String[] args){    System.out.println(lastDigitFactorial(6));}} // This code is contributed by coder001
# Python3 program to find last digit in# factorial n. def lastDigitFactorial(n):         # Explicitly handle all numbers    # less than or equal to 4    if (n == 0): return 1    elif (n <= 2): return n    elif (n == 3): return 6    elif (n == 4): return 4         # For all numbers greater than 4    # the last digit is 0    else: return 0 print(lastDigitFactorial(6)) # This code is contributed by divyeshrabadiya07
// C# program to find last// digit in factorial n.using System;class GFG{     static int lastDigitFactorial(int n){         // Explicitly handle all numbers    // less than or equal to 4    if (n == 0) return 1;    else if (n <= 2) return n;    else if (n == 3) return 6;    else if (n == 4) return 4;         // For all numbers greater than    // 4 the last digit is 0    else return 0;} // Driver codepublic static void Main(string[] args){    Console.Write(lastDigitFactorial(6));}} // This code is contributed by rutvik_56
<script> // JavaScript program to find last digit in// factorial n. function lastDigitFactorial(n){    // Explicitly handle all numbers    // less than or equal to 4    if (n == 0) return 1;    else if (n <= 2) return n;    else if (n == 3) return 6;    else if (n == 4) return 4;         // For all numbers greater than 4    // the last digit is 0    else return 0;}     // Driver code     document.write(lastDigitFactorial(6));     // This code is contributed by Surbhi Tyagi </script>
0
 
Time Complexity : O(1)Auxiliary Space : O(1)
Now try below problems
1) Last non-zero digit in factorial2) Count zeroes in factorial3) First digit in Factorial
coder001
divyeshrabadiya07
rutvik_56
surbhityagi15
factorial
number-digits
Mathematical
School Programming
Mathematical
factorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Modulo Operator (%) in C/C++ with Examples
Program to find GCD or HCF of two numbers
Merge two sorted arrays
Prime Numbers
Program to find sum of elements in a given array
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
Interfaces in Java