Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How to separate number using integer division and modulus operator?

swtxmeiswtxmei Posts: 16Member
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

  • GiantGiant Posts: 225Member
    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
    :


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


  • Fish DogFish Dog Posts: 3Member
    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
    :
    :


  • swtxmeiswtxmei Posts: 16Member
    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
    : :
    : :
    :
    :


  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [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]

  • blitzblitz Posts: 620Member
    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.