# First digit in factorial of a number

Given a positive integer n, find the first digit in its factorial.

**Examples :**

Input : n = 5 Output : 1 Factorial of 5 is 120 and first digit is 1. Input : 1000 Output : 4

A **simple solution** is to compute factorial of number, then find first digit in it.

The above solution causes **overflow** soon. A better solution is to use the fact that factorial contains trailing 0s and removing trailing 0s does not change first digit. For example, first digit of x * y is same as x * y * 100 for x > 0 and y > 0.

## C++

`// A CPP program for finding the First digit ` `// of the large factorial number ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `firstDigit(` `int` `n) ` `{ ` ` ` `long` `long` `int` `fact = 1; ` ` ` ` ` `for` `(` `int` `i = 2; i <= n; i++) { ` ` ` `fact = fact * i; ` ` ` ` ` `// Removing trailing 0s as this ` ` ` `// does not change first digit. ` ` ` `while` `(fact % 10 == 0) ` ` ` `fact = fact / 10; ` ` ` `} ` ` ` ` ` `// loop for divide the fact until it ` ` ` `// become the single digit and return ` ` ` `// the fact ` ` ` `while` `(fact >= 10) ` ` ` `fact = fact / 10; ` ` ` ` ` `return` `fact; ` `} ` ` ` `// derive main ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` `cout << firstDigit(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// A Java program for finding the First digit ` `// of the large factorial number ` `class` `GFG{ ` `static` `int` `firstDigit(` `int` `n) ` `{ ` ` ` `int` `fact = ` `1` `; ` ` ` ` ` `for` `(` `int` `i = ` `2` `; i <= n; i++) { ` ` ` `fact = fact * i; ` ` ` ` ` `// Removing trailing 0s as this ` ` ` `// does not change first digit. ` ` ` `while` `(fact % ` `10` `== ` `0` `) ` ` ` `fact = fact / ` `10` `; ` ` ` `} ` ` ` ` ` `// loop for divide the fact until it ` ` ` `// become the single digit and return ` ` ` `// the fact ` ` ` `while` `(fact >= ` `10` `) ` ` ` `fact = fact / ` `10` `; ` ` ` ` ` `return` `fact; ` `} ` ` ` `// derive main ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `5` `; ` ` ` `System.out.println(firstDigit(n)); ` `} ` `} ` `//This code is contributed by Smitha Dinesh Semwal ` |

*chevron_right*

*filter_none*

## Python3

`# Python program for finding ` `# the First digit of the ` `# large factorial number ` `import` `math ` `def` `firstDigit(n) : ` ` ` `fact ` `=` `1` ` ` ` ` `for` `i ` `in` `range` `(` `2` `, n ` `+` `1` `) : ` ` ` `fact ` `=` `fact ` `*` `i ` ` ` ` ` `# Removing trailing 0s ` ` ` `# as this does not ` ` ` `# change first digit. ` ` ` `while` `(fact ` `%` `10` `=` `=` `0` `) : ` ` ` `fact ` `=` `int` `(fact ` `/` `10` `) ` ` ` ` ` `# loop for divide the fact ` ` ` `# until it become the single ` ` ` `# digit and return the fact ` ` ` ` ` `while` `(fact >` `=` `10` `) : ` ` ` `fact ` `=` `int` `(fact ` `/` `10` `) ` ` ` ` ` `return` `math.floor(fact) ` ` ` `# Driver Code ` `n ` `=` `5` `print` `(firstDigit(n)) ` ` ` `# This code is contributed by ` `# Manish Shaw(manishshaw1) ` |

*chevron_right*

*filter_none*

## C#

`// A C# program for finding the First digit ` `// of the large factorial number ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `static` `int` `firstDigit(` `int` `n) ` ` ` `{ ` ` ` `int` `fact = 1; ` ` ` ` ` `for` `(` `int` `i = 2; i <= n; i++) ` ` ` `{ ` ` ` `fact = fact * i; ` ` ` ` ` `// Removing trailing 0s as this ` ` ` `// does not change first digit. ` ` ` `while` `(fact % 10 == 0) ` ` ` `fact = fact / 10; ` ` ` `} ` ` ` ` ` `// loop for divide the fact until ` ` ` `// it become the single digit and ` ` ` `// return the fact ` ` ` `while` `(fact >= 10) ` ` ` `fact = fact / 10; ` ` ` ` ` `return` `fact; ` ` ` `} ` ` ` ` ` `// driver function ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 5; ` ` ` ` ` `Console.Write(firstDigit(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by parashar. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program for finding ` `// the First digit of the ` `// large factorial number ` ` ` `function` `firstDigit(` `$n` `) ` `{ ` ` ` `$fact` `= 1; ` ` ` ` ` `for` `(` `$i` `= 2; ` `$i` `<= ` `$n` `; ` `$i` `++) ` ` ` `{ ` ` ` `$fact` `= ` `$fact` `* ` `$i` `; ` ` ` ` ` `// Removing trailing 0s as this ` ` ` `// does not change first digit. ` ` ` `while` `(` `$fact` `% 10 == 0) ` ` ` `$fact` `= ` `$fact` `/ 10; ` ` ` `} ` ` ` ` ` `// loop for divide the fact ` ` ` `// until it become the single ` ` ` `// digit and return the fact ` ` ` ` ` `while` `(` `$fact` `>= 10) ` ` ` `$fact` `= ` `$fact` `/ 10; ` ` ` ` ` `return` `floor` `(` `$fact` `); ` `} ` ` ` `// Driver Code ` `$n` `= 5; ` `echo` `firstDigit(` `$n` `); ` ` ` `// This code is contributed by aj_36. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

1

The above code also fails for slightly higher values. The best idea seems to be to find factorial of large number and then find first digit.

## Recommended Posts:

- Maximum number with same digit factorial product
- Find the last digit when factorial of A divides factorial of B
- Last non-zero digit of a factorial
- Count of Numbers in Range where first digit is equal to last digit of the number
- Largest number less than N with digit sum greater than the digit sum of N
- Factorial of a large number
- C Program for factorial of a number
- Program for factorial of a number
- Sum of divisors of factorial of a number
- Number of digits in N factorial to the power N
- Smallest number S such that N is a factor of S factorial or S!
- Find sum of digits in factorial of a number
- Python Program for factorial of a number
- Calculate MDAS Factorial of given number
- Smallest number with at least n digits in factorial

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.