Find the First Digit of a Number
Find the leading (most significant) digit of an integer using arithmetic.
Keep dividing the number by 10 until it drops below 10. What remains is a single digit — the first (leading) digit. Work with the absolute value so negatives return the same leading digit as their positive counterpart.
Problem Statement
Given an integer, return its first (leftmost, most significant) digit. For
example the first digit of 5824 is 5.
Getting the last digit is easy with % 10; the first digit takes a little
more work because you must remove every digit after it. Use the absolute value
so -917 returns 9, and do not convert the number to a String.
Input: A single integer n.
Output: A single digit (0-9) — the leading digit of n.
Examples
Input: 5824
Output: 5Removing 8, 2 and 4 leaves the leading digit 5.
Input: -917
Output: 9The sign is ignored; the leading digit of 917 is 9.
Constraints
-2,147,483,648 <= n <= 2,147,483,647No String conversion
Think Before You Code
Reveal the questions to ask yourself first
- The last digit is n % 10 — what operation isolates the first digit instead?
- How do you know when only the leading digit is left?
- What condition should the loop keep running under?
- Do negatives change the leading digit?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Peel digits off the right until only the leading digit remains.
- Take the absolute value of
n. - While the value is 10 or greater:
value = value / 10— remove the current last digit.
- When the loop ends the value is between 0 and 9 — that is the first digit.
- Return it.
Dry Run
Walk through the example step by step
Finding the first digit of n = 5824:
step | value | value >= 10? | action
-----+-------+--------------+-------------------
1 | 5824 | yes | value = 5824 / 10 = 582
2 | 582 | yes | value = 582 / 10 = 58
3 | 58 | yes | value = 58 / 10 = 5
end | 5 | no | first digit = 5
Solution
Reveal the full Java solution
public class FirstDigit {
public static int firstDigit(int n) {
long value = Math.abs((long) n);
while (value >= 10) {
value /= 10;
}
return (int) value;
}
public static void main(String[] args) {
System.out.println(firstDigit(5824)); // 5
System.out.println(firstDigit(-917)); // 9
}
}
Where the last digit is exposed instantly by % 10, the first digit is
uncovered by repeatedly discarding the last digit with / 10 until nothing but
the leading digit is left. The loop condition value >= 10 is exactly "there is
still more than one digit," so it halts the moment a single digit remains. The
absolute value guarantees the sign never affects the result.
O(d) where d is the number of digits (log10 n)Space: O(1)Common Mistakes
- Using n % 10, which returns the last digit rather than the first.
- Looping while (value > 10) instead of >= 10, so an input like 10 returns 10 rather than 1.
- Applying Math.abs to the int directly and overflowing for Integer.MIN_VALUE.
Edge Cases to Test
- Single-digit numbers return themselves (the loop never runs).
- n = 0 returns 0.
- Numbers like 10 or 100 return 1, since the trailing zeros are divided away.
Interview Follow-Ups
- How would you get the first digit in O(1) using Math.log10 and Math.pow?
- How would you return the first two digits instead of just the first?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →