# Recursive; n cents with quarters, dimes, nickels...counter help

Page 1 of 1

## 1 Replies - 366 Views - Last Post: 29 April 2013 - 05:31 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=319879&amp;s=9adc4de51c59fd5ed09efcf2ce087912&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 klrre

Reputation: 0
• Posts: 7
• Joined: 04-March 13

# Recursive; n cents with quarters, dimes, nickels...counter help

Posted 29 April 2013 - 04:31 PM

I was hoping to get some helps with my recursion work. I know it's probably a simple solution. Basically I need to count how many times each part of the code runs and have it print; however, everytime I put it into the loop I end up with either a loop that prints out each iteration rather than the total..or an inifinite loop of 1's. (at least those are the two things that happen everytime i change my code). Any help on how to get a counter to work would be much appreciated. I've commented in where I thought they should go...
```public class denomination{

public static int makeChange(int c, int denom) {
//int count=0? but would reset for every iteration. should it go outside instead?
int next_denom = 0;
switch (denom) {
case 25:
next_denom = 10;
//count ++;
break;
case 10:
next_denom = 5;
//count ++;
break;
case 5:
next_denom = 1;
//count ++;
break;
case 1:
return 1;
}
int ways = 0;
for (int i = 0; i * denom <= c; i++) {
ways += makeChange(c - i * denom, next_denom);

}
return ways;
//return count;?
}

public  static void main(String []args){

System.out.println(makeChange(1000, 25));

}
}

```

Is This A Good Question/Topic? 0

## Replies To: Recursive; n cents with quarters, dimes, nickels...counter help

### #2 andrewsw

• But the opposite, you said.

Reputation: 5538
• Posts: 21,860
• Joined: 12-December 12

## Re: Recursive; n cents with quarters, dimes, nickels...counter help

Posted 29 April 2013 - 05:31 PM

It could be a static variable of the class:

```public class Denomination {
static int count;    // initialized to 0 once only
```

(Java classes should be capitalized).

You can examine the value using the class-name: Denomination.count

The tricky thing is how, and when, to reset it to 0.