Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

basic C

Name: Anonymous 2010-11-24 12:17

can you help create a program that will accept an amount and output its denomination.

example

amount: 5670.45

1000: 5
500: 6
200: 0
100: 6
50: 1
20: 1
10: 0
.25: 1
.05: 3

Name: Anonymous 2010-11-24 23:24

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
  unsigned long d[] = {10000, 5000, 2000, 1000, 500, 100,
                       25, 10, 5, 1, 0};
  unsigned long x, *p, n;
  char *s;

  if (argc < 2)
    x = 0;
  else {
    x = 100 * strtoul(argv[1], &s, 10);
    if (*s == '.')
      x += strtoul(s+1, NULL, 10);
  }

  for (p = d; *p; p++)
    if (n = x / *p) {
      printf("%3lu.%02lu: %lu\n", *p / 100, *p % 100, n);
      x -= n * *p;
    }

  return 0;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List