split an integer from 1234 to 1 2 3 4 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

split an integer from 1234 to 1 2 3 4

i want to make a program that splits tye integers for example 1234 to 1 2 3 4 and then show their sum and display the the splitted nums 1 2 3 4

3rd Feb 2020, 4:13 AM
sameer
sameer - avatar
4 Answers
+ 1
this can be useful for you String.valueOf(); str.toCharArray(); for sum += nchar - 48; // ASCII 48: '0' .print(nchar +" ");
3rd Feb 2020, 6:00 AM
zemiak
+ 5
Another approach can be Convert integer to string and use // Python n=list('1234') print(n) // [ '1' , '2' , '3' , '4']
3rd Feb 2020, 6:03 AM
Rstar
Rstar - avatar
+ 2
You can either take a string or integer input and then convert it to the required type but remember that it has to be in the form of an integer when you try to find the sum of its elements which can be achieved by a simple while loop. May be for split display you can convert the input to a char array and print it one by one to the console.
3rd Feb 2020, 4:57 AM
Avinesh
Avinesh - avatar
0
Hi! you can use the operation to find the remainder (%) of the number. for example, to separate units from another integer, divide the number by 10 (%10). example on Python: Number = 1234 Unit = Number % 10
3rd Feb 2020, 5:13 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar