Tutorialspoint
Problem
Solution
Submissions

Find the Last Digit of a Number

Certification: Basic Level Accuracy: 72.18% Submissions: 133 Points: 5

Write a Python program that finds the last digit of a given integer.

Example 1
  • Input: 12345
  • Output: 5
  • Explanation:
    • Step 1: Take the input number 12345.
    • Step 2: Calculate 12345 % 10 = 5 to get the last digit.
    • Step 3: Return 5 as the result.
Example 2
  • Input: -6789
  • Output: 9
  • Explanation:
    • Step 1: Take the input number -6789.
    • Step 2: Calculate abs(-6789) = 6789 to get the absolute value.
    • Step 3: Calculate 6789 % 10 = 9 to get the last digit.
    • Step 4: Return 9 as the result.
Constraints
  • -10^9 ≤ number ≤ 10^9
  • For negative numbers, return the absolute value of the last digit
  • Time Complexity: O(1)
  • Space Complexity: O(1)
StringsFunctions / MethodsMicrosoftShopify
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use modulo operator: abs(number) % 10
  • Convert to string and get last character: int(str(abs(number))[-1])
  • Use math.fmod() for floating point numbers: int(abs(math.fmod(number, 10)))
  • Create a function: def last_digit(num): return abs(num) % 10

The following are the steps to find the last digit of a number:

  • Take an integer as input.
  • Use `num % 10` to extract the last digit.
  • Print the last digit.
  • Handle cases like negative numbers.

Submitted Code :