# How to find the first and last n/2-digits from an n-digit number?

I have an n-digit number, say X. Now, what-is-the/how-can-I-come-up-with-an equation [or function] that I should use to get the first n/2-digit number and second n/2-digit numbers from the n-digit number?

For example:

X = 1234 ; n = 4
a = 12 (first n/2 digits)
b = 34 (second n/2 digits)

X = 56789 ; n = 5
a = 567 (first n/2 digits - ceiling)
b = 89 (second n/2 digits)

-
You should post it on scicomp.stackexchange.com (if you are looking for an algorithm that helps in writing a program) – Kirthi Raman Mar 15 '12 at 11:26
Well, I can write one in C++ as this was one of my very first programming exercises, before I really gave up. It is not particularly hard to put this into a program if you know what the syntax is. But, yes I think this site is not well-suited for more technical questions about coding and algorithms. – user21436 Mar 15 '12 at 11:28

## 2 Answers

when $n=1234$, for instance $x = \lceil \log_{10}(n) \rceil = 4$, also what if the number were $4321$ instead of $1234$ ? How would you use this $x$ to get the first portion you are interested in?

$\displaystyle{\left\lceil \frac{n}{10^{x/2}} \right\rceil}$ gives you the first portion if $x$ here were even, and if $x$ were odd then $\displaystyle{\left\lceil \frac{n}{10^{(x+1)/2}} \right\rceil}$ gives you the first portion

Use similar logic to explore the second half of what you want.

(Check Wolfram Alpha to get to understand floor and ceil functions better) http://tinyurl.com/6tvhq9s

-
It looks like an obvious one now! .. duh!! Thanks! :) – Sangeeth Saravanaraj Mar 15 '12 at 11:58
Good, I know with that hint you can get it – Kirthi Raman Mar 15 '12 at 12:03
It would help to be explicit that $x$ counts the number of digits in a given number. – user21436 Mar 15 '12 at 12:13
Well, this was not going to reveal much. It would have been more helpful to the OP if he knew why this works. On a completely unrelated note, you could use the @ before user's name if you're replying to them. – user21436 Mar 15 '12 at 12:21
@KVRaman Why do you delete a message that was here? Yes you have the right to, but not when it will outrightly make the next comment pointless! – user21436 Mar 30 '12 at 12:41

It would be the quotient and remainder when divided by $k$ where

$$k=\begin{cases} \dfrac n 2, \mbox{n is even} \\\\ \dfrac{n+1}{2}, \mbox{n is odd}\end{cases}$$

If you're writing a computer program, you may also want to determine $n$ by having a loop tell you when the remainder by $10^n$ is no more an integer.

-
@Downvoter Care to explain. – user21436 Mar 30 '12 at 12:37