Are there any tips and tricks i maybe can use to optimise this program, less declarations of variable, less lines, less loops ?; so i could invest effort later.

#include <cs50.h>
#include <stdio.h>
#include <math.h>

int main(void) {

puts("Please input a non-negative value!");
float change_in_dollars;
do {
    printf("Enter value:");
    change_in_dollars = get_float();
while (change_in_dollars < 0);

int cents = (int) round(change_in_dollars*100);

int quarters = 25;
int dimes = 10;
int nickles = 5;
int pennies = 1;
int coin = 0;

while (cents>=quarters) {
    cents -= quarters;

while (cents>=dimes) {
    cents -= dimes;

while (cents>=nickles) {
    cents -= nickles;

while (cents>=pennies) {
    cents -= pennies;


1 Answer 1


Your code is correct, but there are some more effective ways to design it.

First, your variables. You don't have to make variables for quarters, dimes, nickels, or pennies, since those don't have to be recorded. A shorter way would simply be to use the numbers that the variables represent (25, 10, 5, 1). Since you don't need to print out these variables, it's easier just to write the numbers, especially since you only write each variable twice in your entire code, not including where you declare the variable.

Second, your while loop. You loop each statement and then move on to the next loop. If you want less loops, wrap all the loops into one while loop and then turn the other loops into if/else statements. Here is an example:

 while(change > 0)
    if (change >= 25)
        // code
    else if(change >= 10)
        // code
    else if(change >= 5)
        // code
    else if(change >= 1)
        // code

Note this is more code, but it is only one while loop instead of four.

One thing you could do to better format your question is add comments, which will help non-coders understand your program better.

Comment below if you still have questions.

Is it possible to use the modulo (%) in Greedy?

Though this is an uncommon method, it isn't technically impossible. You could do something a little bit like this:

// when change is less than 25, change % 25 will return change
if (change % 25 != change)
    change = change % 25;
    // implement coin_count

While it is possible to use the modulo to find out the remainder after dividing, it's more difficult to implement a way to count the coins. There are a few things you could do, like rounding, but I'll leave the solution up to you!

I hope this helped!

  • I really appreciate your help, Thanks. Indeed I agree with you on the point of the variables, once i read your answer i immediately recalled the lecture when Malan said :" try not to create a variable and then just immediately try to return it's value just once", so thanks for referring me to that point. Of course from now on i'll try to add comments on each step as that helps other people and even help me grasp the logic of my code better. You also made me realize the good use of the conditions here. However, do you know how can i use the "modulo" here ? Thanks for your effort and time. Feb 7, 2017 at 8:29
  • I really don't know what to say, i really appreciate your help so much and i think i somehow get the idea of how the modulo would work in this case. Yup, i'm a newbie coder and this is my first ever cs and programming course but i love challenges and have no problems spending many hours over days dedicated to figure out a solution. I'll try to modify my code and tackle that problem and if it worked, i'll definitely notify you; i just have to search more about how modulo works and try to implement few simple programs first. Thank you so much for you effort and sorry for replying so late :D Feb 8, 2017 at 9:53
  • I'm glad I was able to help you! If you want to go the modulo route, I definitely suggest waiting until you progress more in the class. Feb 8, 2017 at 12:44
  • I agree with you, by then i should be able to understand more. Feb 9, 2017 at 12:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .