Thread: Notes of Different Dominations

  1. #1
    Registered User
    Join Date
    Jan 2020
    Posts
    3

    Notes of Different Dominations

    Q) Write a program to determine the smallest number of notes of different domination that are required for an amount input by a user. Assume that the notes are in domination of 100,50,20,10,5,2 and 1.

    Code:
    #include<stdio.h>
    int main()
    {
        int amount,x;
        int dom[7]={100,50,20,10,5,2,1};
        int num[7]={0,0,0,0,0,0,0};
        printf("Enter the amount:");
        scanf("%d",&amount);
        x=amount;
        for(int i=0;i<7;i++){
            while(x>=dom[i]){
                x-=dom[i];
                num[i]++;
            }
        }
        printf("The min no of note for amount % d is when there are:\n",amount);
        for(int j=0;j<7;j++){
            if (num[j]!=0)
                 printf("%d notes of %d\n",num[j],dom[j]);
        }
        return 0;
    }
    I would like to know if this is good or someother more efficient code can be written.
    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,738
    You can replace the inner while loop with suitable application of / and %
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Board Notes
    By webmaster in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-14-2010, 10:31 AM
  2. segment notes
    By cs32 in forum C Programming
    Replies: 2
    Last Post: 03-05-2008, 03:30 AM
  3. PC-Lint, Notes 964 and 966
    By Mario F. in forum Tech Board
    Replies: 1
    Last Post: 06-30-2006, 01:00 PM
  4. Help check my notes.
    By MB1 in forum C++ Programming
    Replies: 4
    Last Post: 03-15-2005, 03:11 PM
  5. Any Notes?
    By COXWELL in forum C Programming
    Replies: 1
    Last Post: 04-11-2002, 10:24 PM

Tags for this Thread