Find amount to be added to achieve target ratio in a given mixture in C++



Suppose we have a container with size X. It has a mixture of water and other liquid, the mixture has W% of water in it. We have to find how many water must be added to increase the ratio of water to Y%? If X = 125, W = 20 and Y = 25, then output will be 8.33 liters.

Suppose we have to add A amount of water with the previous mixture, so new amount will be X + A. So the amount of water in the mixture will follow this formula.

Old Amount+A=((W% of X) + A)

Also the amount of water in the mixture = new percentage of water in the new mixture. So this is Y% of (X + A).

So we can express it like − Y% of (X + A)  = (W % of X) + A

A = [X * (Y - W)] / [100 - Y]

Example

 Live Demo

#include<iostream>
using namespace std;
float getWaterAmount(float X, float W, float Y) {
   return (X * (Y - W)) / (100 - Y);
}
int main() {
   float X = 125, W = 20, Y = 25;
   cout << "We need "<< getWaterAmount(X, W, Y) << " liters of water";
}

Output

We need 8.33333 liters of water

Advertisements