please help me create a program using modulus%

This is a discussion on please help me create a program using modulus% within the C Programming forums, part of the General Programming Boards category; that when i enter a number ,for example 575, the results would be like this... 500: 1 100: 0 50: ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    5

    please help me create a program using modulus%

    that when i enter a number ,for example 575,
    the results would be like this...

    500: 1
    100: 0
    50: 1
    20: 1
    10: 0
    5: 5
    1: 0

    it would give the breakdown just like what is in the above...

    our instructor said we will use modulus%

    thank you...

  2. #2
    Registered User
    Join Date
    Jun 2010
    Posts
    27
    Take like these:

    number=575;
    printf("%d: %d\n",500,number/500);
    number%=500;
    printf("%d: %d\n",100,number/100);
    number%=100;
    printf("%d: %d\n",50,number/50);
    number%=50;
    printf("%d: %d\n",20,number/20);
    number%=20;
    printf("%d: %d\n",10,number/10);
    number%=10;
    printf("%d: %d\n",5,number/5);
    number%=5;
    printf("%d: %d\n",1,number/1);

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    45
    if you just give him the code to read in the number from the user, you will have given him the entire answer

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    5
    thanks steve cao,it's working

    here's my program

    # include <stdio.h>
    main()
    {
    int num, x;
    clrscr();
    printf("Enter a number");
    scanf("%d",&x);
    num=x;
    printf("%d: %d\n",500,num/500);
    num%=500;
    printf("%d: %d\n",100,num/100);
    num%=100;
    printf("%d: %d\n",50,num/50);
    num%=50;
    printf("%d: %d\n",20,num/20);
    num%=20;
    printf("%d: %d\n",10,num/10);
    num%=10;
    printf("%d: %d\n",5,num/5);
    num%=5;
    printf("%d: %d\n",1,num/1);
    getch();
    }

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    27
    It is nothing.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That's nice, but it's not using modulus, and not what the instructor is wanting, apparently.

    Looks like he/she expects you to "strip" off the rightmost digit, one at a time, using modulus, and division with the number line basics.
    Code:
    place = 1;
    number = 575;
    while(number) {
      575 %  10 == 5 * place; //one's
      place *= 10;
      now divide 575 by 10, which removes the right-most digit.
      
    }
    /* 
      which would loop thru this:
      57 % 10 == 7 tens
      divide 57 by 10
    
      5 == 5 hundreds.
    */
    Now you work out the exact bills or coins for this amount, if you need to do that.

    Look back at Steve's code, and ask yourself, "could that code handle 18 dollars, as is?".

    That's the lesson your teacher is trying to teach, imo.
    Last edited by Adak; 06-22-2010 at 06:05 AM.

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    27
    Thanks!Adak.

  8. #8
    Registered User
    Join Date
    Jun 2010
    Posts
    27
    //thanks Adak.

    Code:
    place=10;
    while(number){
       printf("%d\n",number%place);
       number/=place;
    }

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,621
    Steve, stop posting full solutions to problems. The OP needs to figure some stuff out him/herself. We are here to provide help and guide, not give out full solutions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Except that's not really what the instructor is doing. Look at the first post again. I'm assuming they have some actual plan in mind, and aren't just deciding what to divide by at random.

    First they divide by 500, then by 100.
    They do the same thing for the 1s place. Divide by 5, and by 1.
    Why they throw a 20 in there isn't clear though. They divide by 50 and again by 10, but they also throw in a 20.

    It's almost like they expect:

    Pull a digit off.
    Divide by 5.
    Divide by whatever modulus is left, if it is not zero.
    Divide by 1.
    Go to the next digit.


    That's the best I can get from it, but they're not just pulling off one digit at a time.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Jun 2010
    Posts
    5
    well thanks for posting people...
    btw,our instructor told us we won't use loop,if and then,and the likes...
    only %,/,-
    thanks to all...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM
  3. How to create a file association program?
    By eShain in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2006, 11:15 AM
  4. Trying to create a payroll calculation program
    By nadeni0119 in forum C++ Programming
    Replies: 1
    Last Post: 04-01-2003, 03:14 PM
  5. program won't create status bar
    By Unregistered in forum Windows Programming
    Replies: 7
    Last Post: 08-30-2001, 04:00 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21