+ 5

Challenge!!! Write a digits divider.

Write a program that gets an integer number and separates the digits that compose it. Examples: 12568 -> 1 2 5 6 8 -829 -> - 8 2 9 Do not use string.

25th Sep 2017, 12:58 PM
Andrea Simone Costa
Andrea Simone Costa - avatar
17 Respostas
25th Sep 2017, 10:41 PM
Andrea Simone Costa
Andrea Simone Costa - avatar
+ 11
https://code.sololearn.com/c90oIFOzV7LW/#cs my solution
26th Sep 2017, 12:57 AM
Vukan
Vukan - avatar
+ 6
26th Sep 2017, 10:49 AM
Swapnil Srivastava
Swapnil Srivastava - avatar
+ 3
@luka good, but i said "Do not use string." ;)
25th Sep 2017, 1:23 PM
Andrea Simone Costa
Andrea Simone Costa - avatar
+ 3
Is the output an array of integer numbers? eg.: [1,2,5,6,8]
25th Sep 2017, 2:05 PM
Calviղ
Calviղ - avatar
+ 3
@Baptiste E. Prunier nice way XD but...
25th Sep 2017, 2:21 PM
Andrea Simone Costa
Andrea Simone Costa - avatar
+ 3
@Megatron, more space between digits XD & i got some problems with negative numbers
25th Sep 2017, 2:43 PM
Andrea Simone Costa
Andrea Simone Costa - avatar
25th Sep 2017, 6:58 PM
ysraelcon
ysraelcon - avatar
+ 2
C : #include <stdio.h> int main(){ char c; while((c=getchar()) != '\n') printf("%c ",c); putchar('\n'); return 0; }
25th Sep 2017, 2:11 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 2
Edit: Sure more spaces I forgot Negative I will look in( 45 mins later)** I have that logic for negative as well*** Some important work**** #include <iostream> using namespace std; int main() { int number, original, digit, i, n; cin>>number; original=number; for(i=0;number>0;number/=10,i++); for(n=1;i>1;n*=10,i--); while(n>=1) { digit=(original/n)%10; cout<<digit<<" "; n/=10; } return 0; }
25th Sep 2017, 2:39 PM
Megatron
Megatron - avatar
+ 2
Use function prototyping: Number.prototype.divider = function() { var p=Array.from(n+'').map(Number); return n>0?p:p.map((v,i)=>i===1?-1*v:v).splice(1); } alert(12568.divider()); https://code.sololearn.com/WkBrITk3Gj6g/?ref=app
25th Sep 2017, 2:54 PM
Calviղ
Calviղ - avatar
+ 2
Works with negative number also #include <iostream> using namespace std; int main() { int number, original, digit, i, n, isneg; cin>>number; if(number < 0) { isneg=-1; number*=-1; } original=number; for(i=0;number>0;number/=10,i++); for(n=1;i>1;n*=10,i--); while(n>=1) { digit=(original/n)%10; if(isneg==-1) { digit*=-1; isneg=1; } cout<<digit<<" "; n/=10; } return 0; }
25th Sep 2017, 3:38 PM
Megatron
Megatron - avatar
25th Sep 2017, 5:33 PM
Oleg Olyunin
Oleg Olyunin - avatar
+ 1
@calvin if u want
25th Sep 2017, 2:15 PM
Andrea Simone Costa
Andrea Simone Costa - avatar