Breaking down of numbers

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<iostream>
using namespace std;
int main()
{
  int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o;
double Total=b+d+f+h+j+l+n;
  cout <<"Enter value of a:" <<endl;
  cin>> a;
 
  b= a/5000;
 c= a - (5000 * b);
    cout <<" 5000= "<< b;
    
  d= c/2000;
 e= c - (2000 * d);
   cout << " 2000= "<< d;
   
  f= e/1000;
 g= e - (1000 * f);
  cout << " 1000= "<< f;
  
  h= g/500;
 i= g- (500 * h);
  cout << " 500= " << h;
  
  j= h/200;
 k= h - (200 * j);
 cout << " 200=" << j;
 
  l= k/100;
 m= k - (100 * l);
 cout <<" 100= " << l;
 
  n=m/50;
 o= m - (50 * n);
  cout << " 50= "<< n;
  cout << "Total: " << Total;
}

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.
Last edited on
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
What if I want to get the total of the bills
I've tried counting the result of num but I get wrong output
Please help
total = amount
No you didn't understand, what if I want to get the total of number of bills.
Like in 3500
2000= 1
1000 = 1
500= 1
So the total would be 3 bills
I've tried to use increment but it's not working it gives wrong answer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<iostream>
using namespace std;
int main()
{
    int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o;
    int Total{0}; // <-- INT NOT DOUBLE
    
    cout <<"Enter value of a:" <<endl;
    cin>> a;
    
    a = (a/50) * 50; // <-- ROUND DOWN TO MULTIPLE OF 50
    cout << "a = " << a << '\n';
    
    b= a/5000;
    c= a%5000;
    cout <<" 5000= "<< b;
    
    d= c/2000;
    e= c%2000;
    cout << " 2000= "<< d;
    
    f= e/1000;
    g= e%1000;
    cout << " 1000= "<< f;
    
    h= g/500;
    i= g%500;
    cout << " 500= " << h;
    
    j= i/200; // <--
    k= i%200; // <--
    cout << " 200=" << j;
    
    l= k/100;
    m= k%100;
    cout <<" 100= " << l;
    
    n= m/50;
    o= m%50;
    cout << " 50= "<< n;
    
    Total = b+d+f+h+j+l+n; // <--
    cout << "\nNo. of bills: " << Total << '\n'; // <--
}
Bless you , thanks
Topic archived. No new replies allowed.