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 rupee 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++
// 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 rupee coins = " << Rupees * 1 << endl;     cout << "50 paisa coins = " << Rupees * 2 << endl;     cout << "25 paisa coins = " << Rupees * 4 << endl; Â
    return 0; } |
Java
// 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 rupee 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. |
Python3
# 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 rupee coins = " , Rupees * 1 )     print ( "50 paisa coins = " , Rupees * 2 )     print ( "25 paisa coins = " , Rupees * 4 ) Â
# This code is contributed by # Sanjit_Prasad |
C#
// 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 rupee coins = " + Rupees * 1); Â Â Â Â Console.WriteLine( "50 paisa coins = " + Rupees * 2); Â Â Â Â Console.WriteLine( "25 paisa coins = " + Rupees * 4); } } Â
// This code is contributed by inder_verma |
PHP
<?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 rupee 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. ?> |
Javascript
<script> Â
// Javascript implementation of above approach Â
// function to calculate coin. function coin(totalRupees, X, Y, Z) { Â
    var 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 var totalRupees = 1800; var X = 1, Y = 2, Z = 4; var Rupees = coin(totalRupees, X, Y, Z); document.write( "1 rupee coins = " + Rupees * 1 + "<br>" ); document.write( "50 paisa coins = " + Rupees * 2 + "<br>" ); document.write( "25 paisa coins = " + Rupees * 4 + "<br>" ); Â
</script> |
1 rupee coins = 600 50 paisa coins = 1200 25 paisa coins = 2400
Time Complexity: O(1)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!