CS50x pSet1 Cash Done

Here is the final version of the pSet1 Cash problem. I couldn’t work out how to use the % operator. I may come back to revise the problem if in the future it becomes clearer.

#include <cs50.h>
#include <stdio.h>
#include <math.h>
int main (void)
{
    float input;
    
    int cents;
    int coins;
    
//Prompt user for an amount of change
    do {
        input = get_float("How much change: ");
    }  
    while (input < 0);
    cents = input * 100;
    printf("%0.2i\n", cents);

    
    coins = 0;
    while (cents >= 25)
    {
        cents = cents - 25;
        coins ++;
    }
    
    while (cents >= 10)
    {
        cents = cents - 10;
        coins ++;
    }
    
    while (cents >= 5)
    {
        cents = cents - 5;
        coins ++;
    }
    
    while (cents >= 1)
    {
        cents = cents - 1;
        coins ++;
    }
    printf ("%i\n", coins);

}
    

The final results prints as follows:

How much change: .75
75
3

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s