How to print `1234567890` to `1 2 3 4 5 6 7 8 9 0`

2019-10-03 08:19发布

I am sorry, I do not want to work with string or char*.


Many days ago, I have created a small C code. Example, it allows me to input 1234567890; and, it will print 0 9 8 7 6 5 4 3 2 1 to screen:

int n;
printf("n = ");
scanf("%d", &n);
printf("\n---\n\n m = ");
while (n)
{
    printf("%d ", n % 10);
    n = n / 10;
}
printf("\n\n---\n\n");

Now, I tried to create another script, which it allows me to input 1234567890; and, it will print 1 2 3 4 5 6 7 8 9 0 to screen.

#include <stdio.h>

void main()
{
    int n;
    printf("n = ");
    scanf("%d", &n);


    int i1 = 0, n1 = n;
    while (n1 != 0)
    {
        i1 = i1 + 1;
        n1 = n1 / 10;
    }

    printf("\n---\n\nm = ");

    int n2 = n;
    do
    {
        int k = 1, i2 = i1 - 1;
        while (i2 != 0)
        {
            k = k * 10;
            i2 = i2 - 1;
        }

        n2 = n2 / k;
        i1 = i1 - 1;

        printf("%d ", n2);

    }
    while (i1 != 0);

    printf("\n\n---\n\n");
}

My code does not print what I want to have: 1 2 3 4 5 6 7 8 9 0; it always print many zeros: 1 0 0 0 0 0 0 0 0 0.

Can you show me what mistakes in my code?


How to print 1234567890 to 1 2 3 4 5 6 7 8 9 0 with C?

标签: c
6条回答
爷、活的狠高调
2楼-- · 2019-10-03 08:46

By modifying your first small c code, you can obtain the expected display:

#include <stdio.h>

int main()
{
    int n;
    char sNumToText[80]; // to store n in ascii characters

    printf("n = ");
    scanf("%d", &n); // ask for 'n'

    sprintf(sNumToText,"%d",n); // convert integer to text
    printf("\n---\n\n m = ");
    n=0;
    while (sNumToText[n]!='\0')
    {
        printf("%c ", sNumToText[n]); // print one digit and one space
        n ++;
    }
    printf("\n\n---\n\n");

    return (0);
}
查看更多
聊天终结者
3楼-- · 2019-10-03 08:48
#include <stdio.h>

int main(void){
    int n;

    printf("n = ");fflush(stdout);
    scanf("%d", &n);

    int n1 = n;
    int base = 1;
    while(n1 >= 10){
        n1 /= 10;
        base *= 10;
    }
    printf("\n---\n\nm = ");
    while(base){
        printf("%d ", n1 = n / base);
        n -= n1 * base;
        base /= 10;
    }
    return 0;
}
查看更多
放荡不羁爱自由
4楼-- · 2019-10-03 08:48

One way to print a string with spaces between characters and avoid the extra trailing space on the last character:

void printWithSpaces( const char *input )
{
    // start with zero-length separator
    const char *separator = "";

    while ( *input )
    {
        printf( "%s%c", separator, *input );
        separator = " ";
        input++;
    }
}

That will output the string

"1234567890"

as

"1 2 3 4 5 6 7 8 9 0"
查看更多
唯我独甜
5楼-- · 2019-10-03 08:56

another way to do it using math library:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    int n = 0, n1 = 0, n2 = 0;
    int p = 1;

    printf("n = ");
    scanf("%d", &n);

    n1 = n;
    while(n1/10 != 0)
    {
        p++;
        n1/=10;
    }

    printf("\n---\n\nm = ");

    n1 = n;
    n2 = n1/pow(10,p);
    while(p>=1)
    {       
        n1 = n1 - n2*pow(10,p);
        n2 = n1/pow(10,p-1);
        printf("%d ", n2);
        p--;
    }

    printf("\n\n---\n\n");
}

Or using string conversion:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    int n = 0,i,n1,p;
    char *number;

    printf("n = ");
    scanf("%d", &n);

    n1 = n;
    while(n1/10 != 0)
    {
        p++;
        n1/=10;
    }

    number = malloc(p+1);
    if(number != NULL)
    {
        printf("\n---\n\nm = ");

        memset(number,0,p+1);
        sprintf(number, "%d", n);
        for (i=0;i<p+1;i++)
        {       
            printf("%c ", number[i]);
        }
        free(number);
    }


    printf("\n\n---\n\n");
}

EDIT:

Notice the entered value is int the value should be not exceed the maximum value

Use unsigned int is more correct.

查看更多
来,给爷笑一个
6楼-- · 2019-10-03 09:04

Recursion for the win:

#include <stdio.h>

/* print n (n >= 1) recursively */
void recurse(int n) {
    if (!n) return;
    recurse(n / 10);
    printf("%d ", n % 10);
}

int main(void) {
    recurse(1234567890);
    return 0;
}
查看更多
ゆ 、 Hurt°
7楼-- · 2019-10-03 09:05

The following code works just fine with me if you enter only single digit integer i.e 0 to 9

#include<stdio.h>
#include<conio.h>
int main(void){
int c,i=0,k;
int a[100]; //you can enter upto 100 digits;you can change the size if you want
while( (c = getche()) != 13){ //until you hit the enter your input will be taken
    a[i++] = c;
}
for(k = 0;k<i;k++){
    printf("%c ",a[k]);
}
return 0;
}
查看更多
登录 后发表回答