Last active
December 31, 2018 22:44
-
-
Save gastrodon/91f24d315eafac2ac60a9d4a14102adc to your computer and use it in GitHub Desktop.
Script to make change in c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
/* | |
call format: | |
./name price tender | |
output format: | |
list of denominations and their count | |
*/ | |
int main(int argc, char *argv[]) { | |
static const int denoms[11] = {10000, 5000, 2000, 1000, 500, 200, 100, 25, 10, 5, 1}; | |
int price = atoi(argv[1]); | |
int tender = atoi(argv[2]); | |
for(int i = 0; i < sizeof(denoms) / sizeof(denoms[0]); i++) { | |
if(tender - price >= denoms[i]) { | |
printf("%d bills of %d cents\n", (tender - price) / denoms[i], denoms[i]); | |
tender -= denoms[i] * ((tender - price) / denoms[i]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment