We are given two numbers A and B such that B >= A. We need to compute the last digit of this resulting F such that F = B!/A! where 1 = A, B <= 10^18 (A and B are very large).

Examples:

Input : A = 2, B = 4 Output : 2 Explanation : A! = 2 and B! = 24. F = 24/2 = 12 --> last digit = 2 Input : 107 109 Output : 2

As we know, factorial function grows on an exponential rate. Even the largest data type

cannot hold factorial of numbers like 100. To compute factorial of moderately large numbers, refer this.

Here the given constraints are very large. Thus, calculating the two factorials and later

dividing them and computing the last digit is practically an impossible task.

Thus we have to find an alternate approach to break down our problem. It is known that the last digit of factorial always belongs to the set {0, 1, 2, 4, 6}

The approach is as follows: –

1) We evaluate the difference between B and A

2) If the (B – A) >= 5, then the answer is always 0

3) If the difference (B – A) < 5, then we iterate from (A+1) to B, multiply and store them. multiplication_answer % 10 shall be our answer.

## C++

`// CPP program to find last digit of a number` `// obtained by dividing factorial of a number` `// with factorial of another number.` `#include <iostream>` `using` `namespace` `std;` `// Function which computes the last digit` `// of resultant of B!/A!` `int` `computeLastDigit(` `long` `long` `int` `A, ` `long` `long` `int` `B)` `{` ` ` `int` `variable = 1;` ` ` `if` `(A == B) ` `// If A = B, B! = A! and B!/A! = 1` ` ` `return` `1;` ` ` `// If difference (B - A) >= 5, answer = 0` ` ` `else` `if` `((B - A) >= 5)` ` ` `return` `0;` ` ` `else` `{` ` ` `// If non of the conditions are true, we` ` ` `// iterate from A+1 to B and multiply them.` ` ` `// We are only concerned for the last digit,` ` ` `// thus we take modulus of 10` ` ` `for` `(` `long` `long` `int` `i = A + 1; i <= B; i++)` ` ` `variable = (variable * (i % 10)) % 10;` ` ` `return` `variable % 10;` ` ` `}` `}` `// driver function` `int` `main()` `{` ` ` `cout << computeLastDigit(2632, 2634);` ` ` `return` `0;` `}` |

## Java

`// Java program to find last digit of a number` `// obtained by dividing factorial of a number` `// with factorial of another number.` `import` `java.io.*;` `class` `GFG {` `// Function which computes the last digit` `// of resultant of B!/A!` `static` `int` `computeLastDigit(` `long` `A, ` `long` `B)` `{` ` ` `int` `variable = ` `1` `;` ` ` `if` `(A == B) ` `// If A = B, B! = A! and B!/A! = 1` ` ` `return` `1` `;` ` ` `// If difference (B - A) >= 5, answer = 0` ` ` `else` `if` `((B - A) >= ` `5` `)` ` ` `return` `0` `;` ` ` `else` `{` ` ` `// If non of the conditions are true, we` ` ` `// iterate from A+1 to B and multiply them.` ` ` `// We are only concerned for the last digit,` ` ` `// thus we take modulus of 10` ` ` `for` `(` `long` `i = A + ` `1` `; i <= B; i++)` ` ` `variable = (` `int` `)(variable * (i % ` `10` `)) % ` `10` `;` ` ` `return` `variable % ` `10` `;` ` ` `}` `}` `// driver function` `public` `static` `void` `main(String[] args)` `{` ` ` `System.out.println(computeLastDigit(` `2632` `, ` `2634` `));` `}` `}` `// This article is contributed by Prerna Saini` |

## Python3

`# Python program to find` `# last digit of a number` `# obtained by dividing` `# factorial of a number` `# with factorial of another number.` `# Function which computes` `# the last digit` `# of resultant of B!/A!` `def` `computeLastDigit(A,B):` ` ` `variable ` `=` `1` ` ` `if` `(A ` `=` `=` `B): ` `# If A = B, B! = A! and B!/A! = 1` ` ` `return` `1` ` ` ` ` `# If difference (B - A) >= 5, answer = 0` ` ` `elif` `((B ` `-` `A) >` `=` `5` `):` ` ` `return` `0` ` ` ` ` `else` `:` ` ` ` ` `# If non of the conditions` ` ` `# are true, we` ` ` `# iterate from A+1 to B` ` ` `# and multiply them.` ` ` `# We are only concerned` ` ` `# for the last digit,` ` ` `# thus we take modulus of 10` ` ` `for` `i ` `in` `range` `(A ` `+` `1` `, B ` `+` `1` `):` ` ` `variable ` `=` `(variable ` `*` `(i ` `%` `10` `)) ` `%` `10` ` ` ` ` `return` `variable ` `%` `10` ` ` `# driver function` `print` `(computeLastDigit(` `2632` `, ` `2634` `))` `# This code is contributed` `# by Anant Agarwal.` |

## C#

`// C# program to find last digit of` `// a number obtained by dividing` `// factorial of a number with` `// factorial of another number.` `using` `System;` `class` `GFG {` `// Function which computes the last` `// digit of resultant of B!/A!` `static` `int` `computeLastDigit(` `long` `A, ` `long` `B)` `{` ` ` `int` `variable = 1;` ` ` `// If A = B, B! = A!` ` ` `// and B!/A! = 1` ` ` `if` `(A == B)` ` ` `return` `1;` ` ` `// If difference (B - A) >= 5,` ` ` `// answer = 0` ` ` `else` `if` `((B - A) >= 5)` ` ` `return` `0;` ` ` `else` `{` ` ` `// If non of the conditions are true, we` ` ` `// iterate from A+1 to B and multiply them.` ` ` `// We are only concerned for the last digit,` ` ` `// thus we take modulus of 10` ` ` `for` `(` `long` `i = A + 1; i <= B; i++)` ` ` `variable = (` `int` `)(variable *` ` ` `(i % 10)) % 10;` ` ` `return` `variable % 10;` ` ` `}` `}` `// Driver Code` `public` `static` `void` `Main()` `{` ` ` `Console.WriteLine(computeLastDigit(2632, 2634));` `}` `}` `// This code is contributed by vt_m.` |

## PHP

`<?php` `// PHP program to find last digit of a number` `// obtained by dividing factorial of a number` `// with factorial of another number.` `// Function which computes the last` `// digit of resultant of B!/A!` `function` `computeLastDigit(` `$A` `, ` `$B` `)` `{` ` ` `$variable` `= 1;` ` ` ` ` `// If A = B, B! = A!` ` ` `// and B!/A! = 1` ` ` `if` `(` `$A` `== ` `$B` `)` ` ` `return` `1;` ` ` `// If difference (B - A) >= 5,` ` ` `// answer = 0` ` ` `else` `if` `((` `$B` `- ` `$A` `) >= 5)` ` ` `return` `0;` ` ` `else` ` ` `{` ` ` `// If non of the conditions` ` ` `// are true, we iterate from` ` ` `// A+1 to B and multiply them.` ` ` `// We are only concerned for` ` ` `// the last digit, thus we` ` ` `// take modulus of 10` ` ` `for` `(` `$i` `= ` `$A` `+ 1; ` `$i` `<= ` `$B` `; ` `$i` `++)` ` ` `$variable` `= (` `$variable` `* (` `$i` `% 10)) % 10;` ` ` `return` `$variable` `% 10;` ` ` `}` `}` ` ` `// Driver Code` ` ` `echo` `computeLastDigit(2632, 2634);` `// This code is contributed by ajit` `?>` |

## Javascript

`<script>` `// Javascript program to find last digit of a number` `// obtained by dividing factorial of a number` `// with factorial of another number.` `// Function which computes the last digit` `// of resultant of B!/A!` `function` `computeLastDigit(A, B)` `{` ` ` `let variable = 1;` ` ` `if` `(A == B) ` `// If A = B, B! = A! and B!/A! = 1` ` ` `return` `1;` ` ` `// If difference (B - A) >= 5, answer = 0` ` ` `else` `if` `((B - A) >= 5)` ` ` `return` `0;` ` ` `else` `{` ` ` `// If none of the conditions are true, we` ` ` `// iterate from A+1 to B and multiply them.` ` ` `// We are only concerned for the last digit,` ` ` `// thus we take modulus of 10` ` ` `for` `(let i = A + 1; i <= B; i++)` ` ` `variable = (variable * (i % 10)) % 10;` ` ` `return` `variable % 10;` ` ` `}` `}` `// driver function` ` ` `document.write(computeLastDigit(2632, 2634));` `// This code is contributed by Surbhi Tyagi` `</script>` |

Output:

2

This article is contributed by **Shivani Mittal**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.