How to separate number using integer division and modulus operator?

I have to write a program that inputs a five-digit number, separates the number into its individual digits and prints the digits separated from one another by three spaces each. I have to use the integer division and modulus operators.
For example if the user types in 42339
the output should look like this
4 2 3 3 9

Comments

  • If you know that the number entered will always be 5 digits long, the code is

    #include

    int main(void)
    {
    int number = 42339;

    int div = 10000;
    int single;

    while( div >= 1 )
    {
    single = number/div;
    printf( "%d ", single );

    number = number % div;
    div = div / 10;
    }

    return 0;
    }

    Gaint


    : I have to write a program that inputs a five-digit number, separates the number into its individual digits and prints the digits separated from one another by three spaces each. I have to use the integer division and modulus operators.
    : For example if the user types in 42339
    : the output should look like this
    : 4 2 3 3 9
    :


  • But what if you don't know what the user is goin to type??
    please help me
    thank you


  • All of the messages come straight from files in a sertain DIRECTORY. and I had to trouble reading the info into the link list I just can't create one big list out of the two.

    fish.
    : But what if you don't know what the user is goin to type??
    : please help me
    : thank you
    :
    :


  • I have to write a program that inputs a five-digit number, separates the number into its individual digits and prints the digits separated from one another by three spaces each. I have to use the integer division and modulus operators.

    But you don't know what the user is going to type. how do you write a program like this?





    : All of the messages come straight from files in a sertain DIRECTORY. and I had to trouble reading the info into the link list I just can't create one big list out of the two.
    :
    : fish.
    : : But what if you don't know what the user is goin to type??
    : : please help me
    : : thank you
    : :
    : :
    :
    :


  • [code]
    void PrintSeparated (int nInputValue)
    {
    int nDigits = 0;
    int arrDigits [5];

    while (1) {
    arrDigits [nDigits++] = nInputValue % 10; // Modulus
    nInputValue /= 10;
    if (! nInputValue) {
    break; // We reached zero by always dividing
    }
    }

    // Output backwards:
    for (int i=nDigits-1; i>=0; i--) {
    printf ("%d ", arrDigits [i]);
    }
    printf ("
    ");
    }
    [/code]

  • you should use a single div() call instead of doing two divisions - one for finding the quotient and the other for the remainder ...

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories