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

int main(void)
{
    float change;
    int cents = 0, coin_number = 0, quart = 25, dime = 10, nickel = 5, penny = 1;

    //change owed
    do
    {
         change = get_float("change owed: ");

    }
    while (change < 0 );
    
    //quarter
    for (cents = round (change * 100); cents >= quart; cents -= quart)
    {
        coin_number++;
    }
    
    //dime
    for (cents = round (change * 100); (cents >= dime && cents <= 24); cents -= dime)
    {
        coin_number++;
    }
    
    //nickel
    for (cents = round (change * 100); cents >= nickel && cents < dime; cents -= nickel)
    {
        coin_number++;
    }
    
    //penny
    for (cents = round (change * 100); cents >= penny && cents < nickel; cents -= penny)
    {
        coin_number++;
    }*/
    printf("%i cents\n", cents);
    
   
    printf("%i\n", coin_number);
}
0

For each type of coin, the code starts every for loop with the full amount stored in change. It doesn't reduce the value in change by the value of the coins already counted. So, it will produce the maximum number of each type of coin that can be had, all added together.

For example, 70 cents should be 2 quarters and 2 dimes, or 4 coins. This code will count 2 quarters, then 7 dimes then 14 nickels and 70 pennies. Thats 93 coins!

I'm sure you will know how to fix it.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |
  • thank you so much, i'll try to fix it. – Gloria Isedu Jul 4 at 10:23

You must log in to answer this question.

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