Remove Last Digit in Java
beginner· Control Flow · Loops
Problem
Dividing a number by 10 using integer division discards its last digit, since integer division truncates any remainder.
Given a number, remove its last digit.
Input
45678
Output
Number after removing last digit: 4567
Java Program
Java
public class RemoveLastDigit {
public static void main(String[] args) {
int n = 45678;
int result = n / 10; // integer division drops the last digit
System.out.println("Number after removing last digit: " + result);
}
}Output
Number after removing last digit: 4567
Core Logic
Integer division by 10 shifts every digit one place to the right and drops whatever doesn't fit, which is exactly the last digit.
How It Works
- 1
n / 10performs integer division, discarding any remainder. - 2The remainder discarded here is always the last digit, since dividing by 10 shifts the rest of the digits down by one place.
- 3The result is stored in
resultand printed with a label.
For
n = 45678, 45678 / 10 evaluates to 4567, the original number with its last digit removed.💡
Key Point: This is the same integer-division truncation used everywhere else in Java — no rounding happens, the fractional part (which corresponds to the last digit) is simply dropped.
Key Concepts
integer divisionint