# how to separate integer into single digit?

• (2 Pages)
• 1
• 2

## 17 Replies - 35762 Views - Last Post: 27 May 2011 - 06:46 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=233660&amp;s=6f79f14d6f31e8fa46642846f2d87a2b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 xiaojun

• New D.I.C Head

Reputation: 0
• Posts: 22
• Joined: 21-May 11

# how to separate integer into single digit?

Posted 27 May 2011 - 05:30 AM

for example

1234

how to separate into '1' '2' '3' '4' without using array??

i juz know how to separate if i know got how many digits
but i dont know how to separate the number that input by the user

below is my code to separate number into digits that i know got how many number
```#include <stdio.h>

int main(void)
{
int number = 1234;

int div = 10;
int remainder;

while(number)
{
remainder = number/div;
number = number % div;
div = div / 10;

printf( "%d\n", remainder );
}

return 0;
}

```

could any1 help and give some idea to me??

Is This A Good Question/Topic? 0

## Replies To: how to separate integer into single digit?

• Changed Man With Different Priorities

Reputation: 963
• Posts: 2,382
• Joined: 15-February 11

## Re: how to separate integer into single digit?

Posted 27 May 2011 - 05:36 AM

Instead of 10 here
```int div = 10;
```
you gotta use 1000.

This post has been edited by codeprada: 27 May 2011 - 05:36 AM

### #3 RetardedGenius

• ﻿>>──(Knee)──►

Reputation: 127
• Posts: 555
• Joined: 30-October 10

## Re: how to separate integer into single digit?

Posted 27 May 2011 - 05:36 AM

Modular Arithmetic is your friend! HINT: You've actually done it the wrong way round, you want to do this: d = x % 10 before this: x /= 10.

This post has been edited by RetardedGenius: 27 May 2011 - 05:38 AM

### #4 xiaojun

• New D.I.C Head

Reputation: 0
• Posts: 22
• Joined: 21-May 11

## Re: how to separate integer into single digit?

Posted 27 May 2011 - 05:39 AM

how if i duno the number??

```#include <stdio.h>

int main(void)
{
int number;
int div;
int remainder;

scanf("%d", number);

while(number)
{
remainder = number/div;
number = number % div;
div = div / 10;

printf( "%d\n", remainder );
}

return 0;
}

```

### #5 RetardedGenius

• ﻿>>──(Knee)──►

Reputation: 127
• Posts: 555
• Joined: 30-October 10

## Re: how to separate integer into single digit?

Posted 27 May 2011 - 05:48 AM

xiaojun, on 27 May 2011 - 01:39 PM, said:

how if i duno the number??

```#include <stdio.h>

int main(void)
{
int number;
int div;
int remainder;

scanf("%d", number);

while(number)
{
remainder = number/div;
number = number % div;
div = div / 10;

printf( "%d\n", remainder );
}

return 0;
}

```

If you don't know the length of the number you wish to split? Well use a while loop, when you reach the last digit of x x /= 10 will equal 0.

### #6 xiaojun

• New D.I.C Head

Reputation: 0
• Posts: 22
• Joined: 21-May 11

## Re: how to separate integer into single digit?

Posted 27 May 2011 - 05:57 AM

example??

• Changed Man With Different Priorities

Reputation: 963
• Posts: 2,382
• Joined: 15-February 11

## Re: how to separate integer into single digit?

Posted 27 May 2011 - 06:04 AM

Set a int variable to 1, let's call it div. In your while loop you'll keep dividing by 10 until the number is no greater than 0. After the division you check whether to see if the number is over 0 and if so you will multiply div by 10.

### #8 xiaojun

• New D.I.C Head

Reputation: 0
• Posts: 22
• Joined: 21-May 11

## Re: how to separate integer into single digit?

Posted 27 May 2011 - 06:23 AM

```#include <stdio.h>

int main(void)
{
int number;

int base = 1;
int div = 10;
int remainder;
int reverse = 0;

scanf("%d", &number);

while(number)
{
remainder = number % 10;
reverse = reverse * 10 + remainder;
number = number / 10;

}

while(reverse)
{
remainder = reverse % 10;
reverse = reverse / 10;

printf("%d\n", remainder);
}

return 0;
}

```

the output of tat code is

the number I enter is '01'
the result is '1'

but how i want to display both 0 and 1??

means I enter '01'
the number is split and display
0
1

and will not cut off the 0

• Changed Man With Different Priorities

Reputation: 963
• Posts: 2,382
• Joined: 15-February 11

## Re: how to separate integer into single digit?

Posted 27 May 2011 - 06:37 AM

You should change the value of number because you're going to need it later on in your program. You're printing the numbers separately, just on the same line so it 'appears' to be one number.

Follow these steps to get the value of your divisor.
• Make a function which takes one integer as a parameter and returns one integer
• In the function declare two integer variables, one named holder and another named div
• Initialize div to 1. You'll see why we didn't choose zero latter one
• Assign the variable holder the value of the parameter received.
• Create a while loop that will run while holder is greater than zero
• Assign holder the value of 10 divided by itself
• Make an if statement that checks whether the new value of holder is greater than 0
• If the condition evaluates to true then multiply div by 10. If we had 0 then it wont' work since 0 multiplied by any number is always 0
• Return div

If these steps are followed correctly then the number 3455 will produce 1000 as your divisor which will work for your original post.

### #10 xiaojun

• New D.I.C Head

Reputation: 0
• Posts: 22
• Joined: 21-May 11

## Re: how to separate integer into single digit?

Posted 27 May 2011 - 07:00 AM

```#include <stdio.h>

main()
{
int num;
int holder;
int div = 1;

scanf("%d", &num);

holder = num;

while(holder > 0)
{
holder = holder / 10;

if(holder > 0)
{
div = div * 10;
}

printf("%d", holder);
}
}

```

is it like the code above??
i no use function to do it, but i think can do it without dunction
but i cant get the result i want
can point out what mistake i made??

### #11 xiaojun

• New D.I.C Head

Reputation: 0
• Posts: 22
• Joined: 21-May 11

## Re: how to separate integer into single digit?

Posted 27 May 2011 - 07:35 AM

i get what u mean ady but it still not display the zero

'01'

i wan the result in separate line
'0'
'1'

i did what you mention and i try it '01'
the result is

'1'

only

• Changed Man With Different Priorities

Reputation: 963
• Posts: 2,382
• Joined: 15-February 11

## Re: how to separate integer into single digit?

Posted 27 May 2011 - 08:00 AM

Line #22 doesn't produce your answer so remove it.

Add these lines at the after your while statement and it should work.
```int remainder;
while(number)
{
remainder = number/div;
number = number % div;
div = div / 10;

printf( "%d\n", remainder );
}
return 0;

```

This post has been edited by codeprada: 27 May 2011 - 08:00 AM

### #13 xiaojun

• New D.I.C Head

Reputation: 0
• Posts: 22
• Joined: 21-May 11

## Re: how to separate integer into single digit?

Posted 27 May 2011 - 09:58 AM

do you know how to check the user enter how many number of digits in simple solution??

for example user input 562

the output should be "you enter 3 digit"

```#include <stdio.h>

main{}
{
int num;
int digit;

scanf("%d", &num);

if(.....)
{
}

printf("you enter %d digits\n", digit);
}

```

### #14 RetardedGenius

• ﻿>>──(Knee)──►

Reputation: 127
• Posts: 555
• Joined: 30-October 10

## Re: how to separate integer into single digit?

Posted 27 May 2011 - 10:18 AM

xiaojun, on 27 May 2011 - 05:58 PM, said:

do you know how to check the user enter how many number of digits in simple solution??

for example user input 562

the output should be "you enter 3 digit"

```#include <stdio.h>

main{}
{
int num;
int digit;

scanf("%d", &num);

if(.....)
{
}

printf("you enter %d digits\n", digit);
}

```

It would be the amount of times the loop executes in your previous problem. Please don't take this the wrong way, but are you sure that programming is really for you?

• Changed Man With Different Priorities

Reputation: 963
• Posts: 2,382
• Joined: 15-February 11

## Re: how to separate integer into single digit?

Posted 27 May 2011 - 11:11 AM

xiaojun, on 27 May 2011 - 12:58 PM, said:

do you know how to check the user enter how many number of digits in simple solution??

for example user input 562

the output should be "you enter 3 digit"

Refer to post #9 again. Instead of multiplying div by 10 you increment a integer by 1.