I divide by 5 to determine how many ways there are to use nickles. I could have written a for loop:
for(nickles = 0; (quarters+dimes+nickles) <= n; nickles += 5) ++result; But that would have just executed 1+(n-quarters-dimes)/5 times incrementing result every time. I just computed the result in one step instead. Don On Sep 17, 8:08 am, bharatkumar bagana <bagana.bharatku...@gmail.com> wrote: > @don: > will u pls explain why divided by 5 is used .... > > > > On Fri, Sep 16, 2011 at 3:44 PM, prasanth n <nprasnt...@gmail.com> wrote: > > @Don: > > thanks a lot > > > On Sat, Sep 17, 2011 at 12:28 AM, Don <dondod...@gmail.com> wrote: > > >> The algorithm is to count them, looping over the number of quarters > >> and dimes. Then it is easy to compute the number of nickles which > >> could be used, and the shortfall is made up with pennies. > >> It is very common to see a recursive solution, but that is not > >> necessary or beneficial when the iterative solution is so simple. > >> Don > > >> int result = 0; > >> for(int quarters = 0; quarters <= n; quarters += 25) > >> for(int dimes = 0; (quarters+dimes) <= n; dimes += 10) > >> result += 1 + (n-quarters-dimes) / 5; > > >> On Sep 16, 1:35 pm, prasanth n <nprasnt...@gmail.com> wrote: > >> > Given an infinite number of quarters (25 cents), dimes (10 cents), > >> nickels > >> > (5 cents) and pennies (1 cent), write code to calculate the number of > >> ways > >> > of representing n cents. > >> > also do give an algorithm first.. > > >> > -- > >> > *prasanth* > > >> -- > >> You received this message because you are subscribed to the Google Groups > >> "Algorithm Geeks" group. > >> To post to this group, send email to algogeeks@googlegroups.com. > >> To unsubscribe from this group, send email to > >> algogeeks+unsubscr...@googlegroups.com. > >> For more options, visit this group at > >>http://groups.google.com/group/algogeeks?hl=en. > > > -- > > *prasanth* > > > -- > > You received this message because you are subscribed to the Google Groups > > "Algorithm Geeks" group. > > To post to this group, send email to algogeeks@googlegroups.com. > > To unsubscribe from this group, send email to > > algogeeks+unsubscr...@googlegroups.com. > > For more options, visit this group at > >http://groups.google.com/group/algogeeks?hl=en. > > -- > > **Please do not print this e-mail until urgent requirement. Go Green!! > Save Papers <=> Save Trees > *BharatKumar Bagana* > **http://www.google.com/profiles/bagana.bharatkumar<http://www.google.com/profiles/bagana.bharatkumar> > * > Mobile +91 8056127652* > <bagana.bharatku...@gmail.com> -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.