Hi, I'm new to C and programming in general, but have heard that code should not be repeated if possible, which makes a lot of sense, however I have no idea how to make this code less repetetive. Any input would be greatly appreciated.
int main(void)
{
float dollars = 0;
float real_value = 0;
int cents = 0;
int coin_counter = 0;
do
{
dollars = get_float("change owed: ");
cents = round(dollars*100);
}
while(dollars<0);
I'd like these four for-loops to maybe be one, if possible?
for(int i = 0; cents>=25; i++)
{
cents = cents - 25;
real_value = cents;
coin_counter++;
}
for (int i = 0; cents>=10; i++)
{
cents = cents - 10;
real_value = cents;
coin_counter++;
}
for (int i = 0; cents >= 5; i++)
{
cents = cents - 5;
real_value = cents;
coin_counter++;
}
for (int i = 0; cents >= 1; i++)
{
cents = cents - 1;
real_value = cents;
coin_counter++;
}
printf("coins: %i\n",coin_counter);
printf("leftovers: %f\n", real_value);
}