Hi,

how could you find the last digit in an integer? Thank you.

Recommended Answers

All 3 Replies

Modulo 10?

eg:
54%10 = 4

Modulo 10?

eg:
54%10 = 4

TO expand x % 100 will give you the last 2 digits, and x % 1000 will
give you last 3 digits, and in general :

x % 10^n will give you the last n digits. IN the special case
where n = 0, the result will always be 0, since you ask to get the last "0" digits, which does not make sense.

x % 10^n will give you the last n digits. IN the special casewhere n = 0, the result will always be 0, since you ask to get the last "0" digits, which does not make sense.

Right. Or, mathematically, because the modulo represents the remainder after performing an integer division, which yields the whole number result of the divison. From my example above:

54/10 = 5 and leaving us with a remainder of 4
54%10 = 4

Any number raised to the 0th power (n^0) is equal to 1. One divides evenly into an whole number, so it never has a remainder. Thus, the result of the modulo operation is always 0:

54/1 = 54 with no remainder
54%1 = 0

Following, OP? :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.