The purpose of the code is to calculate the least amount of coins needed to produce correct amount of change given a dollar or cent user input. I'm just starting out and appreciate any pointers in how this code could/should be optimized.
The code works but I feel there is some redundancy with all those "while" loops. Using modulo math was another suggestion in order to solve the problem but I felt more comfortable using this solution for now.
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main (void)
{
// Variables//
float change;
int num = 0;
int quarter = 25;
int dime = 10;
int nickel = 5;
int penny = 1;
// Input of change//
do
{
printf("How much change is owed? ");
change = get_float();
}
while (change <= 0);
printf("%.2f\n",change);
// conversion from dollars to cents (need to look over use of rounding)
int cents = (change * 100);
//Coin count//
while ( cents >= quarter)
{
cents -= quarter;
num++;
}
while (cents >= dime)
{
cents -= dime;
num++;
}
while (cents >= nickel)
{
cents -= nickel;
num++;
}
while (cents >= penny)
{
cents -=penny;
num++;
}
printf("%d\n", num);
}