Skip to content

Instantly share code, notes, and snippets.

@gastrodon
Last active December 31, 2018 22:44
Show Gist options
  • Save gastrodon/91f24d315eafac2ac60a9d4a14102adc to your computer and use it in GitHub Desktop.
Save gastrodon/91f24d315eafac2ac60a9d4a14102adc to your computer and use it in GitHub Desktop.
Script to make change in c
#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