Program to find the count of coins of each type from the given ratio

Given the total number of Rupees in a bag and the ratio of coins. The bag contains only 1 Rs, 50 paise, 25 paise coins in X, Y, Z, ratio. The task is to determine the number of 1 Rs coins, 50 Paise coins, and 25 paise coins So that after summation of all these we again get the total rupees given.

Examples:

Input: totalRupees = 1800, X = 1, Y = 2, Z = 4
Output:  1 rupees coins = 600
         50 paisa coins = 1200
         25 paisa coins = 2400

Input: totalRupees = 2500, X = 2, Y = 4, Z = 2
Output:  1 rupees coins = 555
         50 paisa coins = 1110
         25 paisa coins = 2220

Approach:



Let the ratio in which 1 Rs, 50 paise and 25 paise coin is divided is 1:2:4
Now,
1 Rs coins in a bag is 1x.
50 paise coins in a bag is 2x.
25 paise coins in a bag is 4x.

Now convert these paise into Rupees.
So,
x coins of 1 Rs each, the total value is x rupees.
2x coins of 50 paise i.e (1 / 2) rupees each, the total value is x rupees.
4x coins of 25 paise i.e (1 / 4) rupees each, the total is x rupees.

Therefore,

3x = 1800
x = 600

1 rupess coins = 600 * 1 = 600
50 paisa coins = 600 * 2 = 1200
25 paisa coins = 600 * 4 = 2400

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
  
// function to calculate coin.
int coin(int totalRupees, int X, int Y, int Z)
{
  
    float one = 0, fifty = 0, twentyfive = 0,
          result = 0, total = 0;
  
    // Converting each of them in rupees.
    // As we are given totalRupees = 1800
    one = X * 1;
    fifty = ((Y * 1) / 2.0);
    twentyfive = ((Z * 1) / 4.0);
  
    total = one + fifty + twentyfive;
  
    result = ((totalRupees) / total);
  
    return result;
}
  
// Driver code
int main()
{
    int totalRupees = 1800;
    int X = 1, Y = 2, Z = 4;
  
    int Rupees = coin(totalRupees, X, Y, Z);
  
    cout << "1 rupess coins = " << Rupees * 1 << endl;
    cout << "50 paisa coins = " << Rupees * 2 << endl;
    cout << "25 paisa coins = " << Rupees * 4 << endl;
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of above approach 
  
import java.io.*;
  
class GFG {
   
// function to calculate coin. 
 static int coin(int totalRupees, int X, int Y, int Z) 
  
    float one = 0, fifty = 0, twentyfive = 0
        result = 0, total = 0
  
    // Converting each of them in rupees. 
    // As we are given totalRupees = 1800 
    one = X * 1
    fifty = ((Y * 1) / 2); 
    twentyfive = ((Z * 1) / 4); 
  
    total = one + fifty + twentyfive; 
  
    result = ((totalRupees) / total); 
  
    return (int)result; 
  
// Driver code 
  
    public static void main (String[] args) {
          
    int totalRupees = 1800
    int X = 1, Y = 2, Z = 4
  
    int Rupees = coin(totalRupees, X, Y, Z); 
  
    System.out.println( "1 rupess coins = " + Rupees * 1); 
    System.out.println( "50 paisa coins = " + Rupees * 2); 
    System.out.println( "25 paisa coins = " + Rupees * 4); 
    }
}
//This code is contributed by  inder_verma.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of above approach
  
# function to calculate coin.
def coin(totalRupees, X, Y, Z):
  
    # Converting each of them in rupees.
    # As we are given totalRupees = 1800
    one = X * 1
    fifty = ((Y * 1) / 2.0)
    twentyfive = ((Z * 1) / 4.0)
    total = one + fifty + twentyfive
    result = ((totalRupees) / total)
  
    return int(result)
  
# Driver code
if __name__=='__main__':
    totalRupees = 1800
    X, Y, Z = 1, 2, 4
  
    Rupees = coin(totalRupees, X, Y, Z)
  
    print("1 rupess coins = ", Rupees * 1)
    print("50 paisa coins = ", Rupees * 2)
    print("25 paisa coins = ", Rupees * 4)
  
# This code is contributed by
# Sanjit_Prasad

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of above approach 
using System;
  
class GFG 
{
  
// function to calculate coin. 
static int coin(int totalRupees, int X, 
                int Y, int Z) 
  
    float one = 0, fifty = 0, twentyfive = 0, 
          result = 0, total = 0; 
  
    // Converting each of them in rupees. 
    // As we are given totalRupees = 1800 
    one = X * 1; 
    fifty = ((Y * 1) / 2); 
    twentyfive = ((Z * 1) / 4); 
  
    total = one + fifty + twentyfive; 
  
    result = ((totalRupees) / total); 
  
    return (int)result; 
  
// Driver code 
public static void Main () 
{
    int totalRupees = 1800; 
    int X = 1, Y = 2, Z = 4; 
      
    int Rupees = coin(totalRupees, X, Y, Z); 
      
    Console.WriteLine( "1 rupess coins = " + Rupees * 1); 
    Console.WriteLine( "50 paisa coins = " + Rupees * 2); 
    Console.WriteLine( "25 paisa coins = " + Rupees * 4); 
}
}
  
// This code is contributed by inder_verma

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
// PHP implementation of above approach
  
//function to calculate coin
function coin($totalRupees, $X, $Y, $Z
    $one = 0;
    $fifty = 0;
    $twentyfive = 0;
    $result = 0; 
    $total = 0;
          
    // Converting each of them in rupees.
    // As we are given totalRupees = 1800     
    $one = $X * 1;
    $fifty = (($Y * 1) / 2.0);
    $twentyfive = (($Z * 1) / 4.0);
  
    $total = $one + $fifty + $twentyfive;
  
    $result = (($totalRupees) / $total);
  
    return $result;
      
  
// Driver Code 
$totalRupees = 1800;
$X = 1; 
$Y = 2; 
$Z = 4; 
$Rupees = coin($totalRupees, $X, $Y, $Z);
echo "1 rupess coins = ", $Rupees * 1, "\n";
echo "50 paisa coins = ", $Rupees * 2, "\n";
echo "25 paisa coins = ", $Rupees * 4, "\n";
  
// This code is contributed 
// by Shashank_Sharma. 
?> 

chevron_right


Output:

1 rupess coins = 600
50 paisa coins = 1200
25 paisa coins = 2400


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.