0
$\begingroup$

Is there a way to check if the last digit of a 4-digit (or any digit) number is a 1?

I am trying to make something that checks if the first and last digit of a number are both 1. Suppose our number is 1581. To check if the first digit is 1 is trivial. I can go..

if number - 2000 < 0 AND number - 999 > 0, then the first digit is always one.

Is there a test similar to above to check if the last digit is 1?

EDIT: Just to add more context to the question, we want to find an automated way to check if the number starts and ends with a 1.

$\endgroup$
10
  • 1
    $\begingroup$ What do you mean? If you have the number, you can just look at the last digit. If you want something that sounds fancier, just take the remainder on division by $10$. $\endgroup$
    – lulu
    May 30, 2020 at 21:24
  • $\begingroup$ This is part of an authentication license key that will be installed in our cloud to monitor for our applications. After decoding our provided license key, the 1st and last digit should always be 1. There will be thousands of customers and the key validation engine is a basic calculator that can only do floating point operations such as add, subtract, multiply, and divide $\endgroup$
    – Rui Nian
    May 30, 2020 at 21:27
  • $\begingroup$ Does it have a MOD command? Somthing like MOD(1481,10) =1 $\endgroup$
    – Empy2
    May 30, 2020 at 21:36
  • 1
    $\begingroup$ @RuiNian Why don't you just cast it to a string and verify directly? $\endgroup$ May 30, 2020 at 21:38
  • 1
    $\begingroup$ Or FLOOR or INT that gives the integer part, then $1481-10×FLOOR(1481/10)=1$ $\endgroup$
    – Empy2
    May 30, 2020 at 21:39

2 Answers 2

1
$\begingroup$

If it has MOD then something like $$MOD(1481,10)=1$$.
If it has FLOOR or INT to give the integer part, then $$1481-10×FLOOR(1481/10)=1$$

$\endgroup$
1
$\begingroup$

The following translates easily into most programming languages:

$$1000 \le x < 2000 \land x \mathrel{\mbox{mod}} 10 = 1$$

[And if you can, do tell us why you want to do this?]

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.