+ 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.
17 Respostas
+ 3
My solution in c++
https://code.sololearn.com/cQD67Otrw0BR/?ref=app
+ 11
https://code.sololearn.com/c90oIFOzV7LW/#cs
my solution
+ 6
See my code -
https://code.sololearn.com/cxB3IdJYioig/?ref=app
No string used
+ 3
@luka good, but i said "Do not use string." ;)
+ 3
Is the output an array of integer numbers?
eg.: [1,2,5,6,8]
+ 3
@Baptiste E. Prunier nice way XD but...
+ 3
@Megatron, more space between digits XD & i got some problems with negative numbers
+ 3
my practice on python https://code.sololearn.com/chtPXmPc7u0F/#py
+ 2
C :
#include <stdio.h>
int main(){
char c;
while((c=getchar()) != '\n')
printf("%c ",c);
putchar('\n');
return 0;
}
+ 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;
}
+ 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
+ 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;
}
+ 2
Hi all, it's my try
https://code.sololearn.com/cs4PVBH73DZf/#java
+ 1
@calvin if u want