# Coin Counting Program Problems

• 06-02-2012
kevinandrewsun
Coin Counting Program Problems
New to the forum, and was hoping I could get some help. I'm trying to teach myself my university's introduction to computer science class over the summer so I would be better prepared when I take it for real. One of the programs is to apply a "greedy" approach to coin counting. For example:

If I have \$0.42, I would realize that it can only contain one quarter, giving me \$0.16 left over. That would correlate to one dime with \$0.06 left over. That would result in one nickel and two penny resultantly. The output of the program would be "5," representing the least amount of coins possible to make \$0.42.

So, here is the portion of my attempt that didn't work

Code:

```  // analyzes user input     int quarters = 0;     int dimes = 0;     int nickels = 0;     int pennies = 0;     if (change2 >= 25)         do         {         quarters = change2 / 25;         change2 = change2 % 25;         }         while (change2 >= 25);     if (change2 < 25 || change2 >= 10)         do         {         dimes = change2 / 10;         change2 = change2 % 10;         }         while (change2 < 25 || change2 >= 10);     int total = quarters + dimes + nickels + pennies;     printf("%d\n", total);     printf("%d\n", change2); } ```

I have only included the first two steps, because that's where the program begins to break down. The first section with quarters works fine. The problem is getting the data to transfer to the section on dimes. It just seems not to work. Even when I isolate the dime section, the second "if," it produces nothing. There should be no syntax errors involved.

Thanks so much!
• 06-02-2012
Salem
What does this do - without any of your if / while statements?

Code:

```        quarters = change2 / 25;         change2 = change2 % 25;         dimes = change2 / 10;         change2 = change2 % 10;```
Oh, when you paste code, make sure you paste "text only", and not something pre-formatted (as your post is).
• 06-02-2012
kevinandrewsun
Nevermind, guys. Figured it out. I was missing a semicolon after the first "if" statement. Oh, beginner programmer problems. Thanks everyone!
• 06-02-2012
Salem
> I was missing a semicolon after the first "if" statement.
Huh?

You mean you changed
if (change2 >= 25)
into
if (change2 >= 25);

Nope - that just make the if statement redundant.