Harvard CS50 Problem Set 1: greedy change-making algorithm The goal of this code is to take dollar or cents input from the user and give out minimum number of coins needed to pay that between quarters, dimes, nickels and pennies. If this code can be shortened, how would one do it?

#include &lt;stdio.h&gt;
#include &lt;cs50.h&gt;
#include &lt;ctype.h&gt;
#include &lt;math.h&gt;

int main(void)
{
float b;
do
    {
    printf("please give balance owing: ");
    b = GetFloat();
    }
while (b&lt;0);

int v;
int q = 25;
int d = 10;
int n = 5;
int p = 1;
int count=0;

b=b*100;
int c=round(b);
int balance = c;
printf("%i\n", balance);
//printf("%.2f\n", round(c));



while(balance&gt;=q) 
    {
    v=balance/q;
    count=count+v;
    balance=balance-(q*v);
    }
while(balance&gt;=d) 
    {
    v=balance/d;
    count=count+v;
    balance=balance-(d*v);
    }
while(balance&gt;=n) 
    {
    v=balance/n;
    count=count+v;
    balance=balance-(n*v);
    }
while(balance&gt;=p) 
    {
    v=balance/p;
    count=count+v;
    balance=balance-(p*v);
    }

printf("the number of coins needed is: %i\n", count);
printf("balance after coins paid: %i\n", balance);






}
The first thing that sticks out to me is indentation.  But I'm going to recommend some other changes, so I'll address indentation last.

Let's give our variables some better, more descriptive names, and let's declare our constants as constants.  And since we're taking in a floating point number for input from the user, we need to compare that with other floating point numbers.  Or we need to use it as an integer.

int const kVALUE_QUARTER = 25;
int const kVALUE_DIME = 10;
int const kVALUE_NICKEL = 5;
int const kVALUE_PENNY = 1;

float userInput = -1.0;
int balance = 0;
int coinCount = 0;

do {
    printf("please give balance owing: ");
    userInput = GetFloat();
} while (userInput &lt; 0);

balance = (int)(userInput * 100.0);


Now, we can eliminate all of our loops using the modulo operator (%) and integer division.

// number of quarters:
coinCount += balance / kVALUE_QUARTER;
balance %= kVALUE_QUARTER;

// number of dimes:
coinCount += balance / kVALUE_DIME;
balance %= kVALUE_DIME;

// number of nickels:
coinCount += balance / kVALUE_NICKEL;
balance %= kVALUE_NICKEL;

// number of pennies:
coinCount += balance / kVALUE_PENNY;
balance %= kVALUE_PENNY;


Now let's put it all together and take a look at our indentation:

#include &lt;stdio.h&gt;
#include &lt;cs50.h&gt;
#include &lt;ctype.h&gt;
#include &lt;math.h&gt;

int main(void) {
    int const kVALUE_QUARTER = 25;
    int const kVALUE_DIME = 10;
    int const kVALUE_NICKEL = 5;
    int const kVALUE_PENNY = 1;

    float userInput = -1.0;
    int balance = 0;
    int coinCount = 0;

    do {
        printf("please give balance owing: ");
        userInput = GetFloat();
    } while (userInput &lt; 0);

    balance = (int)(userInput * 100.0);

    // number of quarters:
    coinCount += balance / kVALUE_QUARTER;
    balance %= kVALUE_QUARTER;

    // number of dimes:
    coinCount += balance / kVALUE_DIME;
    balance %= kVALUE_DIME;

    // number of nickels:
    coinCount += balance / kVALUE_NICKEL;
    balance %= kVALUE_NICKEL;

    // number of pennies:
    coinCount += balance / kVALUE_PENNY;
    balance %= kVALUE_PENNY;

    printf("the number of coins needed is: %i\n", count);
    printf("balance after coins paid: %i\n", balance);
}