Hi,
I am new to this forum and to programming for that matter.
I am following the CS50 (computer science) online course
from last year and I have problems with one of the assignments.
I am supposed to write a code to calculate the amount of coins
it takes to give change from certain amount of dollars inputed.
There are only 4 coins used (quarter, dime, nickel and penny).
I have found some other similar programs online but I would really
like to know what's wrong with my code.
And I can't believe that somebody asked identical question a few
minutes before me.
Code:
#include <math.h>
#include <stdio.h>
int main (void)
{
// asks user to enter the amount and veryfies the input
float dollars;
do
{
printf("Enter the amount to get change for:");
scanf("%f", &dollars);
}
while (dollars <= 0);
// converts dollars into cents
int cents = round(dollars * 100);
// calculates the number of coins needed to give change
int quarter = 25;
int dime = 10;
int nickel = 5;
int penny = 1;
int coins = 0;
while (cents >= quarter)
{
cents -= quarter;
coins++;
}
while (cents >= dime)
{
cents -= dime;
coins++;
}
while (cents -= nickel)
{
cents -= nickel;
coins++;
}
while (cents -= penny)
{
cents -= penny;
coins++;
}
printf("%d\n", coins);
return 0;
}
Thanks for any help
Kamil