How would I go about taking a number like 123456
and having it print as 1 2 3 4 5 6
?

- 53,924
- 26
- 111
- 144

- 53
- 1
- 1
- 3
-
I mean how can I implement spaces in printf final display – MusicianCPlus Jan 18 '10 at 08:29
-
I took at shot at reformatting your question. I'm guessing English isn't your first language, so no worries. :) – GManNickG Jan 18 '10 at 08:30
-
Thank you GMan :) Yes exactly this is what I want to achive – MusicianCPlus Jan 18 '10 at 08:32
8 Answers
The simplest way of doing this (though not the fastest) would probably be to first sprintf
the number to a string buffer, and then loop through the buffer printf
-ing one character and one space at a time.
There's no built-in way of doing this within the standard printf
formatting.

- 53,924
- 26
- 111
- 144
-
sprintf in C++ not C? I understand not possible in C and I am yet not very advance in C – MusicianCPlus Jan 18 '10 at 08:49
-
-
4
As 'jamesdlin' has mentioned in his comment, GMan's approach would work, however you will need to store it in a buffer in order to print out in the correct order (his algorithm would print out "6 5 4 3 2 1" for input 123456). At this point I'd say that it would be much simpler to just use sprintf as 'therefromhere' suggested in his answer (if this is not an algorithm class assignment of course).
In my opinion the simplest way to do it would be using recursion, this way you can print out digits in the right order without using buffers.
The recursive implementation is very simple:
void PrintfRecursivly(int number)
{
if (number < 0)
{
number *= -1;
printf("- ");
}
if (number > 10)
{
PrintfRecursivly(number / 10);
printf(" ");
}
printf("%d", number % 10);
}
int main()
{
int number = -78900456;
PrintfRecursivly(number);
return 0;
}
Input:
-78900456
Output:
- 7 8 9 0 0 4 5 6
EDIT: Thanks to Steve Jessop who suggested a correct algorithm for positive integers while I was away. I changed the above method to print out correctly for all ints (positive and negative), without the last space.
Please note that we can avoid checking for negative values in every recursion, by doing the check just once (in the main function or wherever) but I didn't write it because we would lose more on clarity than gain in performance.

- 16,887
- 18
- 67
- 98
-
You'd be unlikely to run out of stack space even for large integers too. – dreamlax Jan 18 '10 at 09:02
-
It won't work right for 101 it wil print 1 1 and for 0 it won't print anything. Instead of if(res>0) you should do if(number > 0) and then it will work for anything except 0 and negative numbers. You could then write another function in which you check if number is 0 and print 0, or if number is negative and then you printf '-' and PrintfRecursivly(-number). – Razvi Jan 18 '10 at 09:06
-
@Razvi : You are not correct, this will work for all int32 numbers greater or equal to 0. (number >= 0). Even for negative numbers (it is true that at the moment it wouldn't work) its a very simple fix, however I'll let that to the MusicianCPlus. – David Božjak Jan 18 '10 at 09:22
-
1It doesn't work for numbers whose first decimal digit is greater than 1. In those cases it prints a spurious "0 " at the start. It also prints a spurious space at the end - the questioner asks for `1 2 3 4 5 6`, not `1 2 3 4 5 6 `. But since the questioner accepted this answer, I guess he doesn't mind that, and when he says "numbers like 123456" he means "numbers starting with 1" ;-) – Steve Jessop Jan 18 '10 at 11:16
-
@Steve Jessop: Yes you are correct, just noted now that with numbers starting with a digit larger than one, '0 ' will be printed as well. You are also correct about the last printed space, but as you said: the questioner accepted the answer so for now I will leave it as it is. If I later get my hands on a C++ compiler, I will correct these mistakes. – David Božjak Jan 18 '10 at 12:30
-
I've edited with my suggested fix, until you get a chance. Hope that's OK. – Steve Jessop Jan 18 '10 at 14:12
-
To continue nitpicking: this now goes wrong for 10 (prints "0"), and 2's complement MIN_INT (prints "- -8" for me). A better way to deal with negative numbers in this kind of thing is to have an outer function which takes a signed int, and an inner function which takes an unsigned int, and hence can cope with the problem that in signed arithmetic -MIN_INT == MIN_INT. – Steve Jessop Jan 19 '10 at 00:32
A common method would be to extract each digit, and then print that digit. I won't give you the code, but it's the implemented version of:
int d; // your number
/* While `d` is not zero */
/* Modulus `d` with 10 to extract the last digit */
/* Print it, with your space */
/* Divide by 10 to remove the last digit */
/* Repeat */
This will be backwards. I'll leave it as an exercise to you to fix that. (Hint: In the loop, put the result into an array of characters, and when you're finished start at the last index of the array and print backwards.)

- 494,350
- 52
- 494
- 543
-
2This is the right approach, but don't forget to reverse the string afterward. – jamesdlin Jan 18 '10 at 08:40
char buffer[50];
int myNum = 123456;
int n;
int i;
n = snprintf(buffer, sizeof buffer, "%d", myNum);
for (i = 0; i < n; i++)
{
putchar(buffer[i]);
putchar(' ');
}
putchar('\n');

- 93,976
- 29
- 161
- 209
int number = 123456;
char strNumber[64];
strNumber[0] = '\0';
sprintf_s(strNumber, "%d", number);
int i = 0;
while(strNumber[i] != '\0')
printf("%c ", strNumber[i++]);

- 61
- 1
- 5
This only works for unsigned integers:
#include <stdio.h>
#include <math.h>
void print_number(unsigned int number) {
int n = number, c = 0, p;
while (n > 0) {
n /= 10;
c++;
}
for (n = c - 1; n >= 0; n--) {
p = pow(10, n);
printf("%d ", number / p);
number -= number / p * p;
}
printf("\n");
}
int main(int argc, char *argv[]) {
print_number(1);
print_number(12);
print_number(123);
print_number(1234);
print_number(12345);
print_number(1234567);
print_number(12345678);
print_number(123456789);
return 0;
}

- 8,388
- 5
- 36
- 66
#include <stdio.h>
int main ()
{
int a, b, c, d, e;
printf("Write a number of four figures\n");
scanf("%d", &a);
printf("Your number is:\n");
b = (a - (a % 1000)) / 1000;
c = ((a - (a % 100)) / 100) - b * 10;
d = ((a - (a % 10)) / 10) - b * 100 - c * 10;
e = (a - b * 1000 - c * 100 - d * 10);
printf("%d\t%d\t", b, c);
printf("%d\t", d);
printf("%d", e);
return 0;
}

- 2,595
- 3
- 34
- 52
#include <stdio.h>
#include <conio.h>
int main ()
{
int n, a, b, c, d, e;
printf("Enter integer number from 0 to 32, 767: ");
scanf("%d", &n);
printf("\n");
a= n/10000%10;
b= n/1000%10;
c= n/100%10;
d= n/10%10;
e= n%10;
printf("%d %d %d %d %d\n", a, b, c, d, e);
RESULTS/OUPUT:
Enter integer number from 0 to 32,767: 12345
1 2 3 4 5