Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split Integer into two separate Integers

Suppose I have

int n=123456;
int x,y=0;

How do I split the integer "n" in two half.

Note : The Total Number of digits in n will always be multiple of 2, e.g. 1234, 4567, 234567, 345621 etc... all have 2,4,6,8 digits. I want to divide them in half.

I am trying with following Code but it's not working, the y variable is holding reversed second part somehow.

int x, y=0, len, digit;
int n=123456;

len=floor(log10(abs(n))) + 1;
x=n;
while((floor(log10(abs(x))) + 1)>len/2)
{
    digit=x%10;
    x=x/10;
    y=(y*10)+digit;
}
printf("First Half = %d",x);
printf("\nSecond Half = %d",y);

When Input is :

n=123456;

Output I am getting :

First Half = 123
Second Half = 654

Output I want :

First Half : 123

Second Half : 456

like image 782
Prateek Avatar asked Aug 14 '15 18:08

Prateek


People also ask

How do you divide an integer into two parts?

Divide it into two strings: For string 1, find all the positions of digit 4 in the string change it to 3 we can also change it to another number. For the second string put 1 at all positions of digit 4 and put 0 at all remaining positions from the 1st position of digit 4 to the end of the string.

How do you split integer numbers?

Another way of separating the digits from an int number is using the toCharArray() method. We will convert the integer number into a string and then use the string's toCharArray() to get the characters' array. Now we can print out all the characters one by one.

Can you split int in C++?

Split an integer number into digits in C++ First, we have declared integer variable “number” in which we have stored the 4 digit number. We have defined an array “digit” whose size is the same as the integer number we have taken. Then we have defined a loop for splitting the input number into digits.


2 Answers

Here is a demonstrative program. It does not use any function except printf.:) Thus it is the simplest solution.

#include <stdio.h>

int main( void )
{
    unsigned int a[] = { 12, 1234, 123456, 12345678, 1234567890 };
    const unsigned int Base = 10;

    for ( size_t i = 0; i < sizeof( a ) / sizeof( *a ); i++ )
    {   
        unsigned int divisor = Base;
        while ( a[i] / divisor > divisor ) divisor *= Base;

        printf( "%u\t%u\n", a[i] / divisor, a[i] % divisor );
    }        
}

The program output is

1       2
12      34
123     456
1234    5678
12345   67890

If you are going to use a signed integer type and negative numbers then the program can look the following way

#include <stdio.h>

int main( void )
{
    int a[] = { -12, 1234, -123456, 12345678, -1234567890 };
    const int Base = 10;

    for ( size_t i = 0; i < sizeof( a ) / sizeof( *a ); i++ )
    {   
        int divisor = Base;
        while ( a[i] / ( a[i] < 0 ? -divisor : divisor ) > divisor ) divisor *= Base;

        printf( "%d\t%d\n", a[i] / divisor, a[i] % divisor );
    }        
}

Its output is

-1      -2
12      34
-123    -456
1234    5678
-12345  -67890
like image 92
Vlad from Moscow Avatar answered Oct 21 '22 20:10

Vlad from Moscow


Here is actually what I would do

#include <stdio.h>
#include <math.h>

int main(void)
{
  int x, y=0, len, digit;
  int n=123456;

  len=floor(log10(abs(n))) + 1;
  x = n / pow(10, len / 2);
  y = n - x * pow(10, len / 2;
  printf("First Half = %d",x);
  printf("\nSecond Half = %d",y);
}
like image 21
soueuls Avatar answered Oct 21 '22 20:10

soueuls