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...
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: ...
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...
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);
if you just give him the code to read in the number from the user, you will have given him the entire answer
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();
}
It is nothing.
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.
Now you work out the exact bills or coins for this amount, if you need to do that.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. */
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.
Thanks!Adak.
//thanks Adak.
Code:place=10; while(number){ printf("%d\n",number%place); number/=place; }
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.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
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.
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...