# Better Coding Way?

• 01-19-2006
Better Coding Way?
Hi, in a book that is teaching me C, I have an exercise to where I need to code a change counter as long as the monitary value is under \$1.00. If you look at the code, you can see what is going on. There is nothing wrong with the code, but I was wondering if there is a better way to do this. I can see already, the way I am going with this is going to become quite tedious. Do you have any suggestions on ways to make it more terse or efficient?

Code:

```#include <stdio.h> int main(int argc, char *argv[]) {         int total;    //users input total         int quarters; //teh amount of quarters         int dimes;    //the amount of dimes         int nickels;  //the amount of nickels         int pennies;  //the amount of pennies         char line[100];    //line fo' input data                 printf("Enter a monitary value less than \$1.00: ");         fgets(line, sizeof(line), stdin);         sscanf(line, "%d", &total);         while (total > 99)         {                 printf("Please enter a monitary value less than 1 dollar: ");                 fgets(line, sizeof(line), stdin);                 sscanf(line, "%d", &total);         }         if (total >= 75)         {                 total = total - 75;                 if (total < 5)                 {                         printf("The change is 3 quarters and %d pennies.\n", total);                 }                         else if (total >= 5)                 {                                                }         }                         return (0); }```
Any help is appreciated.
• 01-19-2006
filker0
Yes, there is a better way.

Quarters have a value of 25.
Dimes have a value of 10.
Nickles have a value of 5.
Pennies have a value of 1.

Use the "/" operator on integers to figure out how many of the largest coin left.
Use the "%" operator to find out how much is left over after using the largest coin.

Do this for each coin size from quarter to nickle, and you'll have the number of each coin, what's left is pennies.
• 01-19-2006
```#include <stdio.h> int main(int argc, char *argv[]) {         int total;    //users input total         int quarters; //teh amount of quarters         int dimes;    //the amount of dimes         int nickels;  //the amount of nickels         int pennies;  //the amount of pennies         char line[100];    //line fo' input data                 printf("Enter a monitary value less than \$1.00: ");         fgets(line, sizeof(line), stdin);         sscanf(line, "%d", &total);         while (total > 99)         {                 printf("Please enter a monitary value less than 1 dollar: ");                 fgets(line, sizeof(line), stdin);                 sscanf(line, "%d", &total);         }         quarters = total / 25;         total = total - (quarters * 25);                 dimes = total / 10;         total = total - (dimes * 10);         nickels = total / 5;         total = total - (nickels * 5);                 pennies = total;         printf("You need %d quarter(s), %d dime(s), %d nickel(s), and %d pennie(s).\n", quarters, dimes, nickels, pennies);         return (0); }```