beginnerDigit ProblemsJava

Find the Sum of the First and Last Digits of a Number

Add the first (leading) and last (trailing) digits of an integer.

Quick Answer

The last digit is number % 10. The first digit comes from dividing the number by 10 until a single digit remains. Grab the last digit before you start dividing, capture the first digit after the loop, then add the two.

Problem Statement

Given an integer, return the sum of its first (leading) and last (trailing) digits. For example, for 5824 the first digit is 5 and the last is 4, so the answer is 9.

Read both digits from the absolute value so negatives behave like their positive form, and do not convert the number to a String.

Input: A single integer n.

Output: An integer — the sum of the first and last digits of n.

Examples

Example 1
Input:  5824
Output: 9

First digit 5 + last digit 4 = 9.

Example 2
Input:  349
Output: 12

First digit 3 + last digit 9 = 12.

Constraints

  • -2,147,483,648 <= n <= 2,147,483,647
  • No String conversion

Think Before You Code

Reveal the questions to ask yourself first
  • Which operation gives the last digit, and which gives the first?
  • Why must you capture the last digit before you start dividing the number down?
  • How do you know the loop has left only the first digit?
  • For a single-digit number, what are its first and last digits?

Hints

Open them one at a time — try after each before revealing the next.

Hint 1
The last digit is n % 10 — read it first, because dividing the number later would destroy it.
Hint 2
The first digit is what remains after dividing by 10 while the value is 10 or more.
Hint 3
Store last, then run the divide-down loop, then read first, then return first + last. Use the absolute value for negatives.

Approach

Reveal the step-by-step approach

Capture the two ends of the number separately.

  1. Take the absolute value of n.
  2. last = value % 10 — record the trailing digit now.
  3. While value >= 10, divide it by 10 to strip trailing digits.
  4. first = value — the leading digit that remains.
  5. Return first + last.

Dry Run

Walk through the example step by step

Summing the first and last digits of n = 5824:

last = 5824 % 10 = 4

step | value | value >= 10? | action
-----+-------+--------------+------------------
1    | 5824  | yes          | value = 582
2    | 582   | yes          | value = 58
3    | 58    | yes          | value = 5
end  | 5     | no           | first = 5

answer = first + last = 5 + 4 = 9

Solution

Reveal the full Java solution
public class SumFirstAndLastDigit {
    public static int sumFirstAndLast(int n) {
        long value = Math.abs((long) n);
        int last = (int) (value % 10);
        while (value >= 10) {
            value /= 10;
        }
        int first = (int) value;
        return first + last;
    }

    public static void main(String[] args) {
        System.out.println(sumFirstAndLast(5824)); // 9
        System.out.println(sumFirstAndLast(349));  // 12
    }
}

The two digits are found with complementary tools: % 10 exposes the trailing digit immediately, while repeated / 10 erodes the number down to its leading digit. The order matters — you must read the last digit before the divide-down loop mutates the value. For a single-digit number the first and last digits are the same digit, so the answer is naturally twice that digit.

Time: O(d) where d is the number of digits (log10 n)Space: O(1)

Common Mistakes

  • Reading the last digit after the divide-down loop, by which point the value is already a single digit.
  • Using while (value > 10) instead of >= 10, mishandling inputs like 10.
  • Applying Math.abs to the int directly and overflowing for Integer.MIN_VALUE.

Edge Cases to Test

  • Single-digit numbers return twice the digit (first equals last).
  • n = 0 returns 0.
  • Numbers ending in 0, like 120, take last = 0 and first = 1, giving 1.

Interview Follow-Ups

  • How would you return the product of the first and last digits instead?
  • How would you compute the first digit in O(1) with Math.log10?

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 →
Chat with us