C++: Get 2 Litres from 4 Litres and 3 Litres of Jar Puzzle

There are two jars/buckets of water with the capacity of 4 litres and 3 litres. Write a program in C++ to get exactly 2 litres of water without using any other jar.

Basic Setup

Firstly, include the header and write the main function in your C++ file.

#include <iostream>

using namespace std;

int main()
{

  return 0;
}

Now inside main function, initialize two integer variables a = 4 and b = 3. Here, variable a depicts the jar a which has 4 litres of water and the variable b depicts the jar b which has 3 litres of water.

int main()
{
  int a = 4;
  int b = 3;
  
  return 0;
}

According to the question, we have to do something such that we are left with 2 litres of water. The process of getting 2 litres of water at the end in either of the jar will require lots of adding and removing of water.

The above processes of adding and removing of water can be simulated with the help of loops. So, we will create a do-while loop. Why do-while loop ? Because, the loop will run at least one time and will terminate once the desired condition is met (in our case, either of the jar having 2 litres of water).

int main()
{
  int a = 4;
  int b = 3;
  
  do
  {
  
  }while(b! = 2);

  return 0;
}

The loop in the above code will terminate once the jar b has 2 litres of water.

Logic Involved

  1. The jar A and jar B have 4 litres and 3 litres of water respectively.
  2. We will empty the jar A and fill it with water in jar B.
  3. Now, jar A contains 3 litres of water and can contain 1 more litre of water (Since it has the capacity of 4 litres).
  4. We will fill the jar B and add the water in jar A from jar B until it is full.
  5. Since, jar A already had 3 litres of water, only 1 litre was lost from jar B which had 3 litres of water.
  6. Hence, we the jar B is left with 2 litres of water.

Coding the Logic

According to our logic, if jar A is full, we have to empty it and fill it with the amount of water in jar B.

  do 
  {
    if(a == 4){
      a = 0;
      continue;
    }
    
    if(a == 0){
      a = b;
      b = 3;
      continue;
    }
    
  } while (b! = 2);

And if the jar A is not full, it has to be filled until it reaches its full capacity.

do 
  {
    if(a == 4){
      a = 0;
      continue;
    }
    
    if(a == 0){
      a = b;
      b = 3;
      continue;
    }
    
    if(a < 4 && a > 0){
      int temp_a = a;
      a = (a+b) - (a+b - 4);
      b = b - (a - temp_a);
    }
  } while (b! = 2);

a = (a+b) – (a+b -4) will keep ensure that the value in a doesn’t exceeds its capacity of 4 litres.

b = b – (a – temp_a) will ensure that only the amount which is added in variable ‘a‘ until it was full is lost from variable b.

Full Code

#include <iostream>

using namespace std;

int main()
{
  int a = 4;
  int b = 3;
  
  do 
  {
    if(a == 4){
      a = 0;
      continue;
    }
    
    if(a == 0){
      a = b;
      b = 3;
      continue;
    }
    
    if(a < 4 && a > 0){
      int temp_a = a;
      a = (a+b) - (a+b - 4);
      b = b - (a - temp_a);
    }
  } while (b! = 2);
  
  cout << "Water in jar B (in litres) : " << b;
  return 0;
}

Github Link

Github link to the code