Hello guys, I'm working on a program which can help me split up a certain number into some sort of pieces.
The pieces are 5000, 2000, 1000, 500, 200, 100,50.
It's a bit hard to explain this but let me give you an example
The user inputs maybe 9950
The program should count the least amount of the pieces I mentioned above are in the number.
For this case it should give out 7.
Whereby
5000= 1
2000= 2
500 = 1
200 = 2
50. = 1
Below is the program tho it's not complete I need ideas on how to do this
It would appear that you calculate Total at the start. Before you've done any calculation. Does that seem right to you? How can you calculate Total right at the very start?
Also: int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o;
What is a? What is b? What is c? And so on.
You're not being charged by the letter. Use variable names that help the reader understand.
You're not writing C code in the year 1985. Don't create all your variables at the start. Create them where it's sensible and meaningful and helpful.
This sounds like you're determining the fewest number of bills or coins needed to represent a given amount of money.
Any time you see yourself making variables like a, b, c, d, e, or val1, val2, val3, etc., you should think about using an array instead.
And any time you see yourself writing the same code over and over with only minor variations, you should consider if a a function or a loop can do the job.
Finally, you can use the modulo operator (%) to get the remainder of a division. So instead of doing c= a - (5000 * b) you can just say c = a % 5000
Putting this all together:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
int bills[] {5000, 2000, 1000, 500, 200, 100, 50 };
int main()
{
int amount;
std::cin >> amount;
for (int bill: bills) {
int num = amount / bill;
amount %= bill;
if (num) {
std::cout << bill << "= " << num << '\n';
}
}
}
Just a reminder, I know my codes seems a little bit funny but I'm not experienced with coding and I'm just learning.
Thanks dhayden for the help and thanks to Repeater for the advice