Given a positive integer n, find the first digit in its factorial.
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.
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.
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.
- Last digit of a number raised to last digit of N factorial
- 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
- Find last digit in factorial
- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Count of Numbers in Range where first digit is equal to last digit of the number
- Find the remainder when First digit of a number is divided by its Last digit
- Largest number less than N with digit sum greater than the digit sum of N
- Program for factorial of a number
- Sum of divisors of factorial of a number
- Factorial of a large number
- C Program for factorial of a number
- Smallest number with at least n digits in factorial
- Number of digits in N factorial to the power N
- One line function for factorial of a number
- Calculate MDAS Factorial of given number
- Smallest number S such that N is a factor of S factorial or S!
- Find the last two digits of Factorial of a given Number
- Find sum of digits in factorial of a 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 firstname.lastname@example.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.