WAP to count total number of notes in given amount

Finding the Number of 500, 100, 50, 20, 10, 5, 2, 1 Rupee Notes in a Given Amount


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. 

Comments

Most Viewed

Write C program to enter any year and check whether year is leap year or not using conditional/ternary operator.

WAP to input week number and print week day name.

C Program to Find Third Angle of a Triangle