beginnerDigit ProblemsJava

Swap the First and Last Digits of a Number

Exchange the leading and trailing digits of an integer and return the new number.

Quick Answer

Find the last digit with % 10, the first digit by dividing down, and the place value of the first digit (a power of 10). Remove the first and last digits from the number, then add the last digit at the front position and the first digit at the ones position.

Problem Statement

Given an integer, swap its first (leading) and last (trailing) digits and return the resulting number. For example 12345 becomes 52341 — the 1 and the 5 trade places.

Work with the absolute value and re-apply the sign at the end. Do not convert the number to a String.

Input: A single integer n.

Output: The integer formed by swapping the first and last digits of n.

Examples

Example 1
Input:  12345
Output: 52341

The leading 1 and trailing 5 swap: 5_234_1.

Example 2
Input:  1234
Output: 4231

The leading 1 and trailing 4 swap: 4_23_1.

Constraints

  • -2,147,483,648 <= n <= 2,147,483,647
  • No String conversion; the swapped result is assumed to fit in an int

Think Before You Code

Reveal the questions to ask yourself first
  • How do you isolate the last digit? The first digit?
  • What is the "place value" of the first digit (its power of ten)?
  • How do you take a digit out of a number and slot another one into its place?
  • What is the answer for a single-digit number?

Hints

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

Hint 1
Last digit = n % 10. First digit = n / firstPlace, where firstPlace is 10 raised to (digitCount - 1).
Hint 2
Compute firstPlace by multiplying by 10 each time you divide the number down to a single digit.
Hint 3
Rebuild: start from n, subtract first*firstPlace and last, then add last*firstPlace (last moves to the front) and first (first moves to the ones place).

Approach

Reveal the step-by-step approach

Move each end digit to the other end using place values.

  1. Take the absolute value of n. If it has a single digit, return n unchanged (nothing to swap).
  2. last = value % 10.
  3. Find firstPlace (the leading digit's place value) and the leading digit by dividing the number down while multiplying firstPlace by 10 each step.
  4. first = value / firstPlace.
  5. Rebuild: result = value - first * firstPlace - last + last * firstPlace + first.
  6. Re-apply the original sign.

Dry Run

Walk through the example step by step

Swapping the first and last digits of n = 12345:

last = 12345 % 10 = 5

find firstPlace by dividing down:
  12345 -> 1234 (firstPlace 10) -> 123 (100) -> 12 (1000) -> 1 (10000)
firstPlace = 10000, first = 12345 / 10000 = 1

result = 12345 - 1*10000 - 5 + 5*10000 + 1
       = 12345 - 10000 - 5 + 50000 + 1
       = 52341

Solution

Reveal the full Java solution
public class SwapFirstAndLastDigit {
    public static int swapFirstAndLast(int n) {
        long value = Math.abs((long) n);
        if (value < 10) {
            return n; // single digit: nothing to swap
        }
        long last = value % 10;
        long firstPlace = 1;
        long temp = value;
        while (temp >= 10) {
            temp /= 10;
            firstPlace *= 10;
        }
        long first = value / firstPlace;
        long result = value - first * firstPlace - last + last * firstPlace + first;
        return (int) (n < 0 ? -result : result);
    }

    public static void main(String[] args) {
        System.out.println(swapFirstAndLast(12345)); // 52341
        System.out.println(swapFirstAndLast(1234));  // 4231
    }
}

Each digit's contribution to a number is digit * placeValue. To swap the ends you remove the first digit's contribution (first * firstPlace) and the last digit's contribution (last, whose place value is 1), then add them back in the opposite places: last * firstPlace puts the old last digit at the front and + first puts the old first digit in the ones place. Computing in long keeps the intermediate last * firstPlace from overflowing while you build the answer.

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

Common Mistakes

  • Forgetting the place value, so the moved digits land in the wrong columns.
  • Not handling single-digit numbers, where there is nothing to swap.
  • Computing last * firstPlace in int, which can overflow for large numbers.

Edge Cases to Test

  • Single-digit numbers are returned unchanged.
  • Numbers ending in 0, like 120, become 021 = 21 (the leading zero vanishes).
  • A swap that moves a large digit to the front can exceed int range for 10-digit inputs and would need a long result.

Interview Follow-Ups

  • How would you swap any two digit positions i and j, not just the ends?
  • How would you guard the result against 32-bit overflow and report it?

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