# Separate an integer into its digits

Page 1 of 1

## 7 Replies - 18762 Views - Last Post: 23 July 2007 - 10:01 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=30695&amp;s=37b630de79db251b1e9d9c4979f6507e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 raedbenz

• New D.I.C Head

Reputation: 0
• Posts: 20
• Joined: 13-December 06

# Separate an integer into its digits

Posted 21 July 2007 - 10:40 AM

Hi...
what is the code/way of separating an integer into its digits. e.g. if i enter the integer 4148,
i want the output

4 1 4 8

Thanks
Is This A Good Question/Topic? 0

## Replies To: Separate an integer into its digits

### #2 scarecrowfeild

• New D.I.C Head

Reputation: 0
• Posts: 33
• Joined: 14-July 07

## Re: Separate an integer into its digits

Posted 21 July 2007 - 11:36 AM

hi!
why don't you use loop.i already write the code and run it.
p/s:dont forget the space.it is pretty simple program.

### #3 zyruz

• New D.I.C Head

Reputation: 1
• Posts: 31
• Joined: 13-August 05

## Re: Separate an integer into its digits

Posted 21 July 2007 - 04:09 PM

you want to use % I bet.

129 % 10 wil be 9, remove 9 from 129, then divide 120 with 10.
then it is 12.
12% 10 = 2. etc etc. until the number is 0.
probarly want to store the new int's in a array.

zy

### #4 no2pencil

• Professor Snuggly Pants

Reputation: 6655
• Posts: 30,975
• Joined: 10-May 07

## Re: Separate an integer into its digits

Posted 21 July 2007 - 04:11 PM

I would just use sprintf & dump it into an array.

### #5 aaa111

• D.I.C Regular

Reputation: 88
• Posts: 284
• Joined: 21-February 07

## Re: Separate an integer into its digits

Posted 21 July 2007 - 11:32 PM

no2pencil, on 21 Jul, 2007 - 04:11 PM, said:

I would just use sprintf & dump it into an array.

Using % operator and loop is great way to solve this problem rather than dump it into an array imo.

### #6 raedbenz

• New D.I.C Head

Reputation: 0
• Posts: 20
• Joined: 13-December 06

## Re: Separate an integer into its digits

Posted 22 July 2007 - 01:17 AM

Quote

hi!
why don't you use loop.i already write the code and run it.
p/s:dont forget the space.it is pretty simple program

could you please specify more or show an example.
Thanks

### #7 aaa111

• D.I.C Regular

Reputation: 88
• Posts: 284
• Joined: 21-February 07

## Re: Separate an integer into its digits

Posted 22 July 2007 - 02:14 AM

raedbenz, on 22 Jul, 2007 - 01:17 AM, said:

Quote

hi!
why don't you use loop.i already write the code and run it.
p/s:dont forget the space.it is pretty simple program

could you please specify more or show an example.
Thanks

this is what i've done but without loop:
```#include<conio.h>
#include<stdio.h>

void main()
{
int a,b,c;

scanf("%d",&a);

b=a%1000;
c=a/1000;
printf("%d",c);

c=b/100;
b=b%100;
printf(" %d",c);

c=b/10;
b=b%10;
printf(" %d",c);

c=b%10;
printf(" %d",c);

getch();
}
```

This post has been edited by aaa111: 22 July 2007 - 02:16 AM

### #8 imamkomc

Reputation: 5
• Posts: 64
• Joined: 09-May 07

## Re: Separate an integer into its digits

Posted 23 July 2007 - 10:01 PM

aaa111, on 22 Jul, 2007 - 02:14 AM, said:

raedbenz, on 22 Jul, 2007 - 01:17 AM, said:

Quote

hi!
why don't you use loop.i already write the code and run it.
p/s:dont forget the space.it is pretty simple program

could you please specify more or show an example.
Thanks

this is what i've done but without loop:
```#include<conio.h>
#include<stdio.h>

void main()
{
int a,b,c;

scanf("%d",&a);

b=a%1000;
c=a/1000;
printf("%d",c);

c=b/100;
b=b%100;
printf(" %d",c);

c=b/10;
b=b%10;
printf(" %d",c);

c=b%10;
printf(" %d",c);

getch();
}
```

If you need loop try this code.

```void main()
{
int a,b,c,check,num[10],length,loop,remainder;

a=4148;
//scanf("%d",&a);

loop=0;
do{
check=powl(10,loop);
loop++;
}while(check<=a);
length=(loop-1);
for(int i=1;i<=length;i++)
{
num[i]=a/powl(10,(loop-2)-(i-1));
remainder=a-num[i]*powl(10,(loop-2)-(i-1));
a=remainder;
cout<<num[i]<<" ";
}
cout<<"\n";
getch();
}

```