0

I thought I had made the right code the behind my logic was to first divide the number with 25 and keeps on dividing it until the answer is less than one which means it cannot be divided any further and then proceed with the remaining amount to get it divided with 10 until it too cant b divided and in all of the process keep track of the counter for number of coins and the remaining amount of money left after each division. Two questions, is this logic right and if yes why am I getting the wrong answers and where are my mistakes?

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

int main(void)
{
    float money;
    int counter;
    float input;
    do
    {
        printf("How much money owed?\n ");
        input = GetFloat();
    }
    while(input <= 0);

    input = input * 100;
    money = roundf (input);
    //printf("%f", money);

    counter = 0;
    while (money / 25 >= 1)
    {
        money = money - money / 25;
        counter++;   
    }

    while (money / 10 >= 1)
    {
        money = money - money / 10;
        counter++;
    }

    while (money / 5 >= 1)
    {
        money = money - money / 5;
        counter++;
    }

    //final number of coins plus pennies
    counter = counter + money;
    printf ("%i \n", counter);
}

2 Answers 2

0

the logic's got a bug.... dividing by the coin amounts gives you the number of coins of that value, which you are then subtracting, like say 10 cents divide by 2 cents gives you 5 meaning five coins of 2 cents each and if u say 10 cents minus five, you get 5 which by your logic you then divide again by 2 blah blah..... giving you around 8...yet the answer should be just five for this example, got by 10 cents minus 2 cents till u get zero, meaning 5 times. Hope this helps.

0

if the user input is $1.6 for your code above, what should be the output?

You must log in to answer this question.

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