Thread: Can someone please guide me I want to make this program to ask denomination .

  1. #1
    Registered User
    Join Date
    Jun 2021
    Posts
    1

    Can someone please guide me I want to make this program to ask denomination .

    Code:
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
        long amount;
        long note5000, note1000, note500, note100, note50, note20, note10, coin5, coin2, coin1;
        clrscr();
    
    
    
        note5000 = note1000 = note500 = note100 = note50 = note20 = note10 = coin5 = coin2 = coin1 = 0;
    
    
    
    
        printf("Enter the amount: ");
        scanf("%ld", &amount);
    
    
    
    
        if(amount >= 5000)
        {
        note5000 = amount/5000;
        amount -= note5000 * 5000;
        }
        if(amount >= 1000)
        {
        note1000 = amount/1000;
        amount -= note1000 * 1000;
        }
        if(amount >= 500)
        {
        note500 = amount/500;
        amount -= note500 * 500;
        }
        if(amount >= 100)
        {
        note100 = amount/100;
        amount -= note100 * 100;
        }
        if(amount >= 50)
        {
        note50 = amount/50;
        amount -= note50 * 50;
        }
        if(amount >= 20)
        {
        note20 = amount/20;
        amount -= note20 * 20;
        }
        if(amount >= 10)
        {
        note10 = amount/10;
        amount -= note10 * 10;
        }
        if(amount >= 5)
        {
        coin5 = amount/5;
        amount -=coin5 * 5;
        }
        if(amount >= 2)
        {
        coin2 = amount/2;
        amount -=coin2 * 2;
        }
        if(amount >= 1)
        {
        coin1 = amount;
        }
    
    
        printf("Total number of notes = \n");
    
    
        textcolor(GREEN);
        cprintf("5000 = %ld\n", note5000);
        textcolor(MAGENTA);
        cprintf("1000 = %ld\n", note1000);
        textcolor(MAGENTA);
        cprintf("500  = %ld\n", note500);
        textcolor(MAGENTA);
        cprintf("100  = %ld\n", note100);
        textcolor(MAGENTA);
        cprintf("50   = %ld\n", note50);
        textcolor(MAGENTA);
        cprintf("20   = %ld\n", note20);
        textcolor(MAGENTA);
        cprintf("10   = %ld\n", note10);
        textcolor(MAGENTA);
        cprintf("5    = %ld\n", coin5);
        textcolor(MAGENTA);
        cprintf("2    = %ld\n", coin2);
        textcolor(MAGENTA);
        cprintf("1    = %ld\n", coin1);
    
    
        getch();
    
    
        return 0;
    }


    Above is my code,I want this program to ask amount and then check the amount and ask in which denominations the money is required depending on the amount and I want to add font style please help me.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    When you start putting numeric suffixes on variables, it's a sure sign you should be thinking about an array.
    Code:
    #include<stdio.h>
    
    #define NUM_DENOMINATIONS 10
    
    int main()
    {
        long amount;
        long denominations[NUM_DENOMINATIONS] = {
          5000, 1000,
          500, 100,
          50, 20, 10,
          5, 2, 1
        };
        long change[NUM_DENOMINATIONS] = { 0 };
    
        printf("Enter the amount: ");
        scanf("%ld", &amount);
    
        for ( int i = 0 ; i < NUM_DENOMINATIONS ; i++ ) {
          if ( amount >= denominations[i] ) {
            change[i] = amount / denominations[i];
            amount -= change[i] * denominations[i];
          }
        }
    
        printf("Total number of notes = \n");
        for ( int i = 0 ; i < NUM_DENOMINATIONS ; i++ ) {
          printf("%ld = %ld\n", denominations[i], change[i] );
        }
    
        return 0;
    }
    > ask in which denominations the money is required
    Like
    Code:
    char required_denominations[100];
    printf("Enter required denominations, eg 50,20,10\n");
    scanf("%s",required_denominations);
    You then parse the char array, and extract the denominations you want.
    I suppose you could start with an empty denominations array (filled with zeros).

    > depending on the amount
    Like for example, they enter an amount of 55 and only allow denominations of 20,10.
    Who gets to keep the 5 ?

    > and I want to add font style please help me.
    Yeah, consoles and terminals don't do fonts.

    Forget about textcolor and any other eye candy until your program is finished and working as you want it.
    Throwing in some decoration is very easy at the end, but a real PITA (and a time waster) to keep maintaining while trying to get the rest of the program working.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    505
    Change making is a difficult problem. It's usually easy to find a trivial solution (all units), and a better algorithm (take the highest note and subtract until you can do no more, then repeat with the next highest note) is also fairly easy. But you can do better.

    What I suggest is that you start with the interface. You need to pass the amount of money, and the notes availbale in, and you need a list of notes out.

    So I suggest

    Code:
    void makchange(long amount, const long *denominations, int Ndenominations, long *notesout)
    You call it like this

    Code:
    long denominations[6] {50, 20, 10, 5, 2, 1}; // notes avialable
    long amount = 210;
    long notes[6]; // where we store the notes
    
    makechange(amount, denominations, 6, notes);
    for (i = 0; i < 6; i++)
    {
        if (notes[i] > 0)
           printf("%ld note : %ld\n" denominations[i], notes[i]);
    }
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 07-14-2012, 09:43 AM
  2. guide me to make this code better or efficient
    By larne in forum C++ Programming
    Replies: 11
    Last Post: 01-19-2009, 05:54 AM
  3. Make window in VB but make program in C/C++?
    By Boomba in forum Windows Programming
    Replies: 1
    Last Post: 06-23-2004, 12:29 AM
  4. How To Make The Program Installed In Program Files Folder?
    By javacvb in forum Windows Programming
    Replies: 4
    Last Post: 11-05-2003, 05:33 PM
  5. Replies: 2
    Last Post: 03-25-2002, 05:49 AM

Tags for this Thread