How can we find the unit digits of a specific number?

Question:

I’m just wondering if there is a code in python to find what is the units digit of a number that I inputed.

Number = int(input('Enter a number:'))
print(Number)
while Number >= 100:
  print(Number-) #That's a minus sign.

Btw yes, I’m trying to do a program which accepts as input a positive integer and checks, using an algorithm, to see whether or not the integer is divisible by 11. This particular test for divisibility by 11 was given in 1897 by Charles L. Dodgson.

So I need to remove the units digit of the number, then subtract the number by the units digit, until it reaches a two digit number, where we calculate if it is divisible by 11.

Asked By: 1010011010

||

Answers:

It looks like the problem description is not complete. The algorithm could be:

  • shorten the number by leaving out the units
  • subtract the units once again from the number
  • multiply the number by -1

The end result will be the number modulo 11, although it could be negative.

num = int(input('Enter a number:'))
print("Given number:   ", num, "   Trying to find num % 11 =", num % 11)
while abs(num) >= 11:
    units = num % 10
    num //= 10  # integer division
    num -= units
    num = -num
    print("Modified number:", num)
if num < 0:
    num += 11
print("Result:", num)

A simplified algorithm, without the pesky num = -num that just checks for divisibility by 11:

num = int(input('Enter a number:'))
print("Given number:   ", num, "   Trying to find num % 11 == 0 :", num % 11 == 0)
given_number = num
while num >= 100:
    units = num % 10  # find the last digit
    num //= 10  # integer division (i.e. remove the last digit)
    num -= units  # subtract the just found last digi
    print("Modified number:", num)

if num in [0, 11, 22, 33, 44, 55, 66, 77, 88, 99]:
    print(given_number, "is divisible by 11")
else:
    print(given_number, "is NOT divisible by 11")
Answered By: JohanC

I assume you refer to this algorithm?

You will need to perform 2 operations in your while loop as you say:

  1. Find the units digit:
unitsDigit = Number % 10

This gives you the number modulo 10, which is the units digit. You can think of it as the remainder after dividing by 10, read more here.

  1. Subtract the units digit from the number without the units digit:
Number = Number // 10

This performs integer division to only give you the quotient from dividing by 10 (hence “deleting” the unit digit).

Number -= unitsDigit

This subtracts the units digit from the shortened number.

Answered By: CH.

It looks like Phantom is following an old trick to determine if a number is divisible by 11. Obviously now you could just use the modulo operator, but lets pretend that we’re doing it the way they did it 100 years ago. For those interested a quick google turned this up:

https://wcipeg.com/problem/ccc96s2

To get it to work we have to flip backwards and forwards treating the number like a string and an integer. As a string we can split the digits up very easily and follow the process.

# From Phantom's code
input_number = int(input('enter a number: '))
print(input_number)

# An intermediate variable to hold the number
worker = abs(input_number)

# for loop that is equal to one less than the number of digits
while worker > 10 :   # Edit - Thanks JohanC for the suggestion in the comments
    # take the last digit and subtract it from the remainging digits
    worker = int(str(worker)[0:(len(str(worker))-1)]) - int(str(worker)[-1])
    print(worker)

# Finish with a print of the divisibility
if worker == 0 : 
    print('The number {} is divisible by 11.'.format(input_number))
else : 
    print('The number {} is not divisible by 11.'.format(input_number))

Hope that helps and is what you are looking for!

Answered By: Cooper

You can find the unit digits by creating a function like this:

def units_digit(num: int|str):
    return str(num)[-1]

It takes an input (which can be an integer or a string) and converts it into a string. It then takes the first character from the right (hence the negative sign), which is also the unit digits. If you want it to return an integer, all you have to do is change it to:

def units_digit(num: int|str):
    return int(str(num)[-1])

Alternatively, if converting to a string is too slow for you, you can do it with integers.

def units_digit(num: float|int)->int:
    return int(num)%10

We convert it to an integer to remove the floating point. Then we find the remainder after dividing by 10. That is the units digit.

Answered By: Jason Grace

You can write a string-based function to extract digits. In such case, you can operate with long numbers.

def extract_digits(number):
    return [int(i) for i in str(number)]

digits = extract_digits(15961)
digits  # [1, 5, 9, 6, 1]

diff_odd_even = sum(digits[1::2]) - sum(digits[0::2])
diff_odd_even # 0
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.