I'm new to programming, and I'm stuck at a problem. I want my program to identify the separate digits in a given number, like if I input 4692
, it should identify the digits and print 4 6 9 2
. And yeah, without using arrays.

- 77,566
- 24
- 149
- 228
-
good interview question. thanks! – George Godik Sep 29 '09 at 21:28
7 Answers
A perfect recursion problem to tackle if you're new to programming...
4692/1000 = 4
4692%1000 = 692
692/100 = 6
692%100 = 92
92/10 = 9
92%10 = 2
You should get the idea for the loop you should use now, so that it works for any number. :)

- 8,488
- 4
- 37
- 30
-
3
-
1Yeah, +1 your answer is good because it leads to the answer rather than just solving the homework, but please correct your numbers! – Jason Cohen Jun 11 '09 at 14:44
-
3The problem with typing too fast, is I type faster than I think sometimes. ;) Corrected! – SuPra Jun 11 '09 at 14:48
Simple and nice
void PrintDigits(const long n)
{
int m = -1;
int i = 1;
while(true)
{
m = (n%(10*i))/i;
i*= 10;
cout << m << endl;
if (0 == n/i)
break;
}
}

- 2,663
- 4
- 29
- 40
Another approach is to have two loops.
1) First loop: Reverse the number.
int j = 0;
while( i ) {
j *= 10;
j += i % 10;
i /= 10;
}
2) Second loop: Print the numbers from right to left.
while( j ) {
std::cout << j % 10 << ' ';
j /= 10;
}
This is assuming you want the digits printed from right to left. I noticed there are several solutions here that do not have this assumption. If not, then just the second loop would suffice.

- 546
- 4
- 11
-
Note part 1 breaks if `i` ends with zeros, because then `j` would stay at zero at the beginning and multiplying it by 10 would not have any effect on it. – Felix Jassler Mar 21 '21 at 15:50
Haven't written C code in year, but this should work.
int i = 12345;
while( i > 0 ){
int nextVal = i % 10;
printf( "%d", nextVal );
i = i / 10;
}

- 12,395
- 3
- 34
- 49
-
This is completely wrong. You print the value of `i` 5 times: "123451234123121". Even if you print `nextVal` instead of `i`, you're doing it in the wrong order and will print "54321". – Jacob Krall Sep 28 '09 at 23:23
-
You should store the result to a string and reverse it before printing. You can also used a stack to reverse – phuclv Aug 02 '13 at 15:03
-
@Babak Naffas's answer is a rather simple yet typical solution. But if you want more speed you can try [double dabble](http://en.wikipedia.org/wiki/Double_dabble) to split the number into BCDs. After that it's easy to take each number out and print. – phuclv Aug 02 '13 at 15:32
Here is a simple solution if you want to just print the digits from the number.
#include <stdio.h>
/**
printdigits
*/
void printDigits(int num) {
char buff[128] = "";
sprintf(buff, "%d ", num);
int i = 0;
while (buff[i] != '\0') {
printf("%c ", buff[i]);
i++;
}
printf("\n");
}
/*
main function
*/
int main(int argc, char** argv) {
int digits = 4321;
printDigits(digits);
return 0;
}

- 652
- 3
- 17

- 1,645
- 1
- 20
- 31
-
1You don't need 128 characters to print an integer. Also, why initialize `buff` if you're just going to overwrite it with `sprintf()` later? Also, why do you have `char *p` if you never use it? – Chris Lutz Sep 29 '09 at 18:21
-
ya, earlier I was planning to iterate using pointer so I have char *p. Regarding size, he did not mentioned length of numbers so to be on safe size, used 128. – rjoshi Sep 29 '09 at 18:26
-
An `int` can only hold numbers up to 4 billion on most platforms, so you should be safe with 10 digits (11 characters with the nul-terminator). If you're worried about 64-bits, that's still only around 20 to 25 characters. – Chris Lutz Sep 29 '09 at 18:30
-
I think the idea is to have non reapeating digits printed (otherwise it would be too simple)... well, you can keep track of the already printed integers without having an array encoding them in another integer.
some pseudo C, to give you a clue:
int encoding = 0;
int d;
while (keep_looking()) {
d = get_digit();
if (encoding/(2**d)%2 == 0) {
print(d);
encoding += 2**d;
}
}

- 74,053
- 25
- 135
- 175
-
-
If I remember right, `**` is Fortran's exponentiation operator. C doesn't have an exponentiation operator. You may replace ``2**d`` with ``1 << d``. – pmg Sep 30 '09 at 00:22
-
that's why I said "pseudo-c", and I didn't want to expose a novice to bit shifting and masking operations (%2 is the same as &1 too), that's why I limited myself to arithmetic operations. – fortran Sep 30 '09 at 09:09
Is it correct
int main()
{
int number;
cin>>number;
int nod=0;
int same=number;
while(same){
same/=10;
nod++;
}
while(nod--){
cout<<(int)number/(int)pow10(nod)%10<<"\t";
}
return 0;
}

- 652
- 3
- 17

- 11
- 3