﻿ Split Integer into Separate Parts - ITCodar

# Split Integer into Separate Parts

## Split integer into separate parts

The two steps to achieve that are

• `split` to have each digit separatly
• print each on a new line

Classic :

``int foo = 123;String[] split = (foo+"").split("");for(int i=0; i<split.length; i++)    System.out.println(split[i]);``

Shorter :

``int foo = 123;Arrays.asList((foo+"").split("")).forEach(System.out::println);``

## Split Integer into two separate Integers

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       212      34123     4561234    567812345   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      -212      34-123    -4561234    5678-12345  -67890``

## How do I separate an integer into separate digits in an array in JavaScript?

Why not just do this?

``var n =  123456789;var digits = (""+n).split("");``

## Separate an integer into two (nearly) equal parts

Just find the first part and subtract it from the original number.

``var x = 7;var p1 = Math.floor(x / 2);var p2 = x - p1;console.log(p1, p2);``

## i want to split the integer into individual elements and add the elements

You can also do this:

``number = int(input("Enter the Number: "))sum_of_digits = 0while number > 0:     digit = number % 10    sum_of_digits = sum_of_digits + digit        number = number // 10``

## C# split integer in parts given part weights algorithm

I suggest rounding while tracking difference (`diff`) between exact `double` value (`v`) and rounded integer one (`value`):

``public static int[] SplitIntoBuckets(int count, int[] weights) {  if (null == weights)    throw new ArgumentNullException(nameof(weights));  else if (weights.Length <= 0)    return new ArgumentOutOfRangeException(nameof(weights), "Empty weights");    double sum = weights.Sum(d => (double)d);  if (sum == 0)    throw new ArgumentException("Weights must not sum to 0", nameof(weights));  Func<double, int> round = (double x) => (int)(x >= 0 ? x + 0.5 : x - 0.5);  int[] result = new int[weights.Length];  double diff = 0;  for (int i = 0; i < weights.Length; ++i) {    double v = count * (double)(weights[i]) / sum;    int value = round(v);    diff += v - value;    if (diff >= 0.5) {      value += 1;      diff -= 1;    }    else if (diff <= -0.5) {      value -= 1;      diff += 1;    }    result[i] = value;  }      return result;}``

Demo:

``string demo = sstring.Join(Environment.NewLine, Enumerable  .Range(200, 15)  .Select(n => \$"{n} = {string.Join(", ", SplitIntoBuckets(n, new int[] { 25, 25, 50 }))}"));Console.Write(demo);    ``

Outcome:

``200 = 50, 50, 100201 = 50, 51, 100202 = 51, 50, 101203 = 51, 51, 101204 = 51, 51, 102205 = 51, 52, 102206 = 52, 51, 103207 = 52, 52, 103208 = 52, 52, 104209 = 52, 53, 104210 = 53, 52, 105211 = 53, 53, 105212 = 53, 53, 106213 = 53, 54, 106214 = 54, 53, 107``

Submit