Here's a fun program in C programming language to find the number of 500, 100, 50, 20, 10, 5, 2, 1 rupee notes in a given amount. It'll surely help cashiers to mange things properly.
# include <stdio.h> # include <conio.h> void main() { int rs, a, b, c, d, e, f, g, h ; clrscr() ; printf("Enter the amount in Rupees : ") ; scanf("%d", &rs) ; while(rs >= 500) { a = rs / 500 ; rs = rs % 500; printf("\nThe no. of five hundreds are : %d", a) ; break ; } while(rs >= 100) { b = rs / 100 ; rs = rs % 100; printf("\n\nThe no. of hundreds are : %d", b) ; break ; } while(rs >= 50) { c = rs / 50 ; rs = rs % 50; printf("\n\nThe no. of fifties are : %d", c) ; break ; } while(rs >= 20) { d = rs / 20 ; rs = rs % 20; printf("\n\nThe no. of twenties are : %d", d) ; break ; } while(rs >= 10) { e = rs / 10 ; rs = rs % 10; printf("\n\nThe no. of tens are : %d", e) ; break ; } while(rs >= 5) { f = rs / 5 ; rs = rs % 5; printf("\n\nThe no. of fives are : %d", f) ; break ; } while(rs >= 2) { g = rs / 2 ; rs = rs % 2; printf("\n\nThe no. of Twos are : %d", g) ; break ; } while(rs >= 1) { h = rs / 1 ; rs = rs % 1; printf("\n\nThe no. of ones are : %d", h) ; break ; } getch() ; }
Output of above program is
Enter the amount in Rupees : 698
The no. of five hundreds are : 1
The no. of hundreds are : 1
The no. of fifties are : 1
The no. of twenties are : 2
The no. of fives are : 1
The no. of Twos are : 1
The no. of ones are : 1
Explanation of above program
Above program is pretty straightforward. All it does is that it takes an amount as input and shows how many 500, 100, 50, 10, 5, 2, 1 notes are needed. As this program contains all basics of C programming, lets understand this program by an example.
Remember: When you divide two integer and stores the value in an integer only integer part of the result is stored e.g. 10 / 3 = 3.333 but when you store this result in another integer, it will only hold 3 as its value (to store the exact result use float as data type).
Also in C, % is modulus and is used to calculate remainder so e.g. 5 % 3 will be 2.
Suppose, rs = 698. Now we go step by step .
- First while loop executes as 698 >= 500, so a = 698 / 500 = 1 and rs = rs % 500 = 698 % 500 = 198. The printf() is executed.
- Next while loop executes as 198 >= 100, so a = 198 / 100 = 1 and rs = rs % 100 = 198 % 100 = 98. The printf() is executed.
- Next while loop executes as 98 >= 50, so a = 98 / 50= 1 and rs = rs % 50 = 98 % 50 = 48. The printf() is executed.
- Next while loop executes as 48 >= 20, so a = 48 / 20= 2 and rs = rs % 20 = 48 % 20 = 8. The printf() is executed.
- The next while loop is not executed because 8 < 10.
- Next while loop executes as 8 >= 5, so a = 8 / 5= 1 and rs = rs % 5 = 8 % 5 = 3. The printf() is executed.
- Next while loop executes as 3 >= 2, so a = 3 / 2= 1 and rs = rs % 2 = 3 % 2 = 1. The printf() is executed.
- Next while loop executes as 1 >= 1, so a = 1 / 1= 1 and rs = rs % 1 = 1 % 1 = 0. The printf() is executed.
plz applications of thisprogram
This program could be used to determine how a sum of money could be formed using notes of different denominations by keeping higher denominations notes as many as possible. E.g. in case the amount is 1500 then three notes of 500
Hi, Tanmay. I made a more efficient code in C++, I hope you enjoy it:
#include
using namespace std;
int main () {
float remaining_value(0),result(0),rest(1);
int aux(0),other(0);
float notes[7] = {100,50,20,10,5,2,1};
cout << "Provide us the value: ";
cin >> remaining_value;
while (rest!=0) {
result = remaining_value/notes[other]; // result in float
aux = result; // number of notes that will be use
rest = remaining_value - (notes[other]*aux); // provides the rest
if (aux!=0) {
cout << aux << " note(s) of " << notes[other] << endl; }
remaining_value = rest;
other++;
}
return 0; }
If you enter an amount that have decimal is it going to work?
I found an alternate approach .. for solving this C program without using multiple while statements. Finding number of notes
i was looking for this type of answer in many sites. but the only satified answer is this. keep it up. well done!
Thank you very much!!.Helped me alot
Hi There,
Thanks for highlighting this and indicating about C Programming Tutorial where more study and thought is necessary.
I work at the biggest internet non-profit company in the world:]. I love flying remote control cars with my boys. Howdy, my first post here. I was told to announce myself. I`m 43 yrs. old, hitched, and have 2 kids under 5. Anyways, that’s about all.
I look forward to see your next updates.
Regards,
Veena
I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.
Php course in chennai
You have explained the topic very well. Thanks for sharing a nice article -visit Java Tutorials
You have explained this topic very well and step by step. Thanks for sharing a nice Post -visit Java Tutorials