Write a program to find the ratio in which a shopkeeper will mix two types of rice worth Rs. kg and Rs. kg, so that the average cost of the mixture is Rs. kg.
Input : X = 50, Y = 70, Z = 65 Output : Ratio = 1:3 Input : X = 1000, Y = 2000, Z = 1400 Output : Ratio = 3:2
According to Alligation rule, the ratio of the weights of two items mixed will be inversely proportional to the deviation of attributes of these two items from the average attribute of the resultant mixture.
w1 / w2 = (d - m) / (m - c)
Below program illustrate the above approach:
$dividend = $a;
$dividend = $b;
// b is greater or equal to a
if($a <= $b) $divisor = $a; else $divisor = $b; while($divisor > 0)
$remainder = $dividend % $divisor;
$dividend = $divisor;
$divisor = $remainder;
// Function to find the ratio of
// two mixtures
function alligation($x, $y, $m)
// Find the cheaper among x and y
if ($x <= $y) $c = $x; else $c = $y; // Find the dearer among x and y if ($x >= $y)
$d = $x;
$d = $y;
// Find ratio r1:r2
$r1 = $d – $m;
$r2 = $m – $c;
// Convert the ration into
// simpler form
$gcd = __gcd($r1, $r2);
echo (int)($r1 / $gcd) . “:” .
(int)($r2 / $gcd);
// Driver code
$x = 50;
$y = 70;
$z = 65;
alligation($x, $y, $z);
// This code is contributed by
// Mukul Singh
- How to solve RSA Algorithm Problems?
- Solve the Logical Expression given by string
- Some Tricks to solve problems on Impartial games
- Fibonacci problem (Value of Fib(N)*Fib(N) - Fib(N-1) * Fib(N+1))
- Josephus problem | Set 2 (A Simple Solution when k = 2)
- Problem of 8 Neighbours of an element in a 2-D Matrix
- Shortest Superstring Problem | Set 2 (Using Set Cover)
- Exact Cover Problem and Algorithm X | Set 1
- Travelling Salesman Problem implementation using BackTracking
- Exact Cover Problem and Algorithm X | Set 2 (Implementation with DLX)
- Maths behind number of paths in matrix problem
- Program to find sum of 1 + x/2! + x^2/3! +...+x^n/(n+1)!
- C Program for ID3 Tagging
- Program to Add Two Complex Numbers
- Program to Find the value of cos(nΘ)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.