C Program to Count Change Program

How to write a C Program to Count Change Program in C Programming Language ?


Solution:

  1.  #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int main(int argc, const char * argv[]) {
  5.    
  6.     float total, lecoins, change;
  7.     int quarters, dimes, nickels, pennies;
  8.     printf("How much change are you owed?");
  9.     scanf("%f", &change);
  10.     if (change <= 0) {
  11.         printf("Invalid constant, try again:");
  12.         scanf("%f", &change);
  13.     }
  14.     lecoins = (int) round(change * 100);
  15.    
  16.     do{ //nested do-while loop.
  17.         while (lecoins >= 25) {
  18.             lecoins = lecoins - 25;
  19.             quarters++;
  20.             total++;
  21.         }
  22.         while (lecoins >= 10) {
  23.             lecoins = lecoins - 10;
  24.             dimes++;
  25.             total++;
  26.         }
  27.         while (lecoins >= 5) {
  28.             lecoins = lecoins - 5;
  29.             nickels++;
  30.             total++;
  31.         }
  32.         while (lecoins >= 1) {
  33.             lecoins = lecoins - 1;
  34.             pennies++;
  35.             total++;
  36.         }
  37.             }
  38.     while (lecoins!=0); {
  39.         printf("\n");
  40.     }
  41.     printf("Quarters: %i \n Dimes: %i \n Nickels: %i \n Pennies: %i\n Total: %f \n", quarters, dimes, nickels, pennies, total);
  42.     return 0;
  43. }


Learn More :