Thread: A little trouble with my self-study homework?

  1. #1
    Registered User
    Join Date
    Jul 2014
    Location
    Philippines
    Posts
    5

    A little trouble with my self-study homework?

    We were asked to write a dollar converter program that accepts a dollar value and convert to its equivalent value in quarters, dimes, nickles and cents.

    and this is as far as I've come to realizing the program... and this is just the base.... we need to convert it using the mod(%) but I have no idea how to...

    Our professor gave us the value of the dollars... (it's because we're not in America or anything)

    1 dollar = 100 cents
    1 quarter = 25 cents
    1 dime = 10 cents
    1 nickle = 5 cents

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int main()
    {
        float ItemPrice, total, cash, change;
        int quantity;
        
        printf("\nAling Puring's Store v.3\n");
        
        printf("\nEnter item price: ");
        scanf("%f", &ItemPrice);
        
        printf("\nEnter quantity: ");
        scanf("%d", &quantity);
        
        total=ItemPrice*quantity;
        
        printf("\nTotal is: %.2f", total);
        
        printf("\n\n******************************\n");
        
        printf("\nCash is: ");
        scanf("%f", &cash);
        
        change=cash-total;
        
        printf("\nChange is: %.2f", change);
        
        getch();
        return 0;
    }
    I ABSOLUTELY HAVE NO IDEA ON HOW TO USE A modulus(%)... and that's the clue our professor gave us

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    First of all, don't use floating point. Use integers. The modulo operator only works for integral types.

    In C, a%b gives the remainder on dividing a by b (assuming both a and b are integral values, and b is non-zero). The mathematical definition is a%b = a - (a/b)*b where the division is integral division with rounding toward zero (i.e. 1/2 gives 0, 2/2 gives 1, 3/2 gives 1, etc).

    Note: Before C99, the rounding of integer division with negative operands was unspecified. C99 and later, rounding is toward zero.

    Second note: One reason not to use floating point for currencies (particularly those with 100 smaller units making up a bigger one) is that floating point variables can't exactly represent all decimal fractions (For example, a floating point variable cannot exactly represent the decimal values 0.1 or 0.01 - those values can only be represented approximately).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    May 2014
    Posts
    23
    hello

    I am studying a book that has this program in, the book is called "ansi c" by steven c lawlor.

    Code:
    #include <stdio.h>
    main()
    {
     int seconds;
     printf("How many seconds? ");
     scanf("%i", &seconds);                                    //input how many seconds
     printf("Days:      %i \n", seconds / 86400);       //seconds divided by how many seconds in a day
     seconds %= 86400;                                        //assign to seconds the remainder after dividing seconds by 86400 (the number of seconds in a day)
     printf("Hours:     %i \n", seconds / 60 / 60);
     seconds %= 3600;
     printf("Minutes:   %i \n", seconds / 60);
     seconds %= 60;
     printf("Seconds:   %i \n", seconds / 1);
    }
    this is how you would use modulo, I had to keep track of 7.73 dollars worth of pennies and I could only have the int pennies and nothing else. so assign a value to pennies = 773.
    then use this value to find dollars,
    print this then use modulo which I think is this,
    pennies %= 700,
    this should make pennies = 73,
    divide this by 50 and print value of 1,
    assign new value to pennies using modulo,
    pennies %= 73

    I think this is right

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What should I study next?
    By Richardcavell in forum C Programming
    Replies: 2
    Last Post: 02-21-2011, 01:05 AM
  2. What should I study
    By santiman12 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 07-15-2008, 08:08 AM
  3. Need study help : /
    By RoD in forum C++ Programming
    Replies: 31
    Last Post: 05-31-2003, 11:03 AM
  4. Replies: 4
    Last Post: 03-02-2003, 09:12 AM
  5. How to start to study C ?
    By riverlam in forum C Programming
    Replies: 9
    Last Post: 10-29-2001, 09:52 AM

Tags for this Thread