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-25 14:54

fixed.

#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 *c, n;
    float x;

    if (argc != 2)
      x = 0;
    else
      x = (unsigned long)(100 * atof(argv[1]));
   
    for (c=d; *c; ++c)
      if (n = x / *c) {
          printf("%3lu.%02lu: %lu\n", *c / 100, *c % 100, n);
          x -= n * *c;
      }

   return 0;
}

I like how the for loop terminates.

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