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
4 odpowiedzi
+ 1
this can be useful for you
String.valueOf();
str.toCharArray();
for
sum += nchar - 48; // ASCII 48: '0'
.print(nchar +" ");
+ 5
Another approach can be
Convert integer to string and use
// Python
n=list('1234')
print(n) // [ '1' , '2' , '3' , '4']
+ 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.
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