Thread: Trying to figure out how the coin denominations are computed.

  1. #1
    Registered User
    Join Date
    Sep 2019
    Posts
    6

    Question Trying to figure out how the coin denominations are computed.

    Code:
    // Import libraries
    #include <stdio.h>
    #include <math.h>
    #include <cs50.h>
    
    int main()
    {
        // Define an int type variable
        int coins_due;
        // Create a 'do while' loop
        do
        {
            float change_owed = get_float("Change owed: ");
            coins_due = round(change_owed * 100);
        }
        while (coins_due <= 0);
    
        int quarters = coins_due / 25;
        int dimes = (coins_due % 25) / 10;
        int nickels = ((coins_due % 25) % 10) / 5;
        int pennies = ((coins_due % 25) % 10) % 5;
    
        // Print output
        printf("Coins due: %d\n", quarters + dimes + nickels + pennies);
    }

  2. #2
    Registered User
    Join Date
    Sep 2019
    Posts
    6
    How is 'quarters' computed ? I think it is : 41 / 25 = 1.64
    How is 'dimes' computed ? Is it 'coins_due' % 25 then that value / 10 , or coins_due % 25 then coins due / 5 ?
    How is 'nickels' computed ? Is it coins_due % 25 then that value is passed to to the right, or coins due against each mathematical step individually ?

  3. #3
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    "without goto we would be wtf'd"

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I'd suggest that you do it by hand a few times

    How you you pay for something worth $1.30...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 09-17-2013, 07:22 PM
  2. coin toss
    By ejd81882 in forum C Programming
    Replies: 3
    Last Post: 10-14-2008, 12:53 PM
  3. coin problem
    By et1wilson in forum C Programming
    Replies: 5
    Last Post: 07-14-2008, 05:19 AM
  4. Pre-computed normals not working
    By VirtualAce in forum Game Programming
    Replies: 4
    Last Post: 06-21-2005, 04:06 PM
  5. Coin Toss
    By kazuakijp in forum C Programming
    Replies: 4
    Last Post: 08-31-2004, 09:02 PM

Tags for this Thread