If n is a given number, how to get the first half of a number and second half? Ex: 9456, what I need is the first half is 94 and the second half is 56.
I can get the first half of a number using (int)(n / pow(10, (digits / 2)))
this formula, but am not able to get the second half. Can anyone help here?
Thank you.
CodePudding user response:
I think you can use the simple process for picking up each digit using n0 and then n/=10 ,then store the first half of numbers in a variable and the other half in another.
CodePudding user response:
first convert number to string , then use .length() to obtain max pow of 10 lets say = p.
then take phalf = p/2 if it is even or (p 1)/2 , if it is odd.
then do divide and modulo with pow( 10, phalf ), divide will give upper first half and modulo woill give lower second half.