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 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
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 = 0
while 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, 100
201 = 50, 51, 100
202 = 51, 50, 101
203 = 51, 51, 101
204 = 51, 51, 102
205 = 51, 52, 102
206 = 52, 51, 103
207 = 52, 52, 103
208 = 52, 52, 104
209 = 52, 53, 104
210 = 53, 52, 105
211 = 53, 53, 105
212 = 53, 53, 106
213 = 53, 54, 106
214 = 54, 53, 107
Related Topics
Consider Defining a Bean of Type in Your Configuration
Why Is My Spring @Autowired Field Null
Java - How to Write My Arraylist to a File, and Read (Load) That File to the Original Arraylist
How to Convert Linkedhashmap to Custom Java Object
How to Get All Keys from a Json-Object as a String Array in Java
How to Check Whether Kafka Server Is Running
Spring Boot Search Data Between Two Dates
What Happens When Base and Derived Classes Each Have Variables With the Same Name
Java 8 Lambda Create List of Strings from List of Objects
Spring Boot - Exception Encountered During Context Initialization. How to Know the Reason of Error
How Many Times Will This Loop Execute
How to Set Default Download Directory in Selenium Chrome Capabilities
How to Get the Part After the Decimal Point in Java
How to Post a Json Payload to a @Requestparam in Spring MVC
Java Regex to Validate Full Name Allow Only Spaces and Letters
How to Create Unique Column With Jpa
Generate Bank Account Number With Random
How to Write Multiple Line Property Value Using Propertiesconfiguration