# Program to check if water tank overflows when n solid balls are dipped in the water tank

Given the dimensions of cylindrical water tank, spherical solid balls and the amount of water present in the tank check if water tank will overflow when balls are dipped in the water tank.

Examples :

input : H = 10, r = 5 h = 5 N = 2, R = 2 output : Not in overflow state Explanation : water tank capacity = 3.14 * r * r * H = 3.14 * 5 * 5 * 10 = 785 volume of water in tank = 3.14 * r * r * h = 3.14 * 5 * 5 * 5 = 392.5 Volume of balls = N * (4/3) * 3.14 * R * R * R = 2 * (4/3) * 3.14 * 2 * 2 * 2 = 67.02 Total volume of water + dip balls = 392.5 + 67.02 = 459.52 Total volume (459.02) < tank capacity (785) So, there is no overflow in tank input : H = 5, r = 3 h = 3 N = 3, R = 2 output : Overflow Explanation: water tank capacity = 3.14 * r * r * H = 3.14 * 3 * 3 * 5 = 141.3 volume of water in tank = 3.14 * r * r * h = 3.14 * 3 * 3 * 3 = 84.78 volume of balls = N * (4/3) * 3.14 * R * R * R = 3 * (4/3) * 3.14 * 2 * 2 * 2 = 100.48 Total volume of water + dip balls = 84.78 + 100.48 = 185.26 Total volume (185.26) > tank capacity (141.3) So, tank will overflow

**Approach:**

When solid balls are dipped in water, level of water increases, hence volume of water will also increase.

Increasing in water volume = Total volume of dip balls

Volume of Cylinder = 3.14 * r * r * h

where: r: radius of tank

h: height of tank

Number of balls are n

Balls have shape of Sphere

Volume of Sphere = (4/3) * 3.14 * R * R * R

Where R: Sphere’s(solid ball) radius

After dipping all balls, if the total volume of water and all balls is less than or equal to the total volume of tank capacity then there will no overflow in tank, otherwise there will be overflow.

Below is the implementation of above approach:

## C++

`// C++ Program to check if water tank ` `// overflows when n solid balls are ` `// dipped in the water tank ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to find if tak will ` `// overflow or not ` `void` `overflow(` `int` `H, ` `int` `r, ` `int` `h, ` ` ` `int` `N, ` `int` `R) ` `{ ` ` ` `// cylinder capacity ` ` ` `float` `tank_cap = 3.14 * r * r * H; ` ` ` ` ` `// volume of water in tank ` ` ` `float` `water_vol = 3.14 * r * r * h; ` ` ` ` ` `// volume of n balls ` ` ` `float` `balls_vol = N * (4 / 3) * 3.14 * R * R * R; ` ` ` ` ` `// total volume of water ` ` ` `// and n dipped balls ` ` ` `float` `vol = water_vol + balls_vol; ` ` ` ` ` `/* condition to check if tank is in ` ` ` `overflow state or not */` ` ` `if` `(vol > tank_cap) { ` ` ` `cout << ` `"Overflow"` `<< endl; ` ` ` `} ` ` ` `else` `{ ` ` ` `cout << ` `"Not in overflow state"` ` ` `<< endl; ` ` ` `} ` `} ` ` ` `// main function ` `int` `main() ` `{ ` ` ` `// giving dimensions ` ` ` `int` `H = 10, r = 5, h = 5, ` ` ` `N = 2, R = 2; ` ` ` ` ` `// calling function ` ` ` `overflow(H, r, h, N, R); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// JAVA Code for Program to check if ` `// water tank overflows ` `import` `java.util.*; ` ` ` `class` `GFG { ` ` ` ` ` `// function to find if tak will ` ` ` `// overflow or not ` ` ` `static` `void` `overflow(` `int` `H, ` `int` `r, ` `int` `h, ` ` ` `int` `N, ` `int` `R) ` ` ` `{ ` ` ` `// cylinder capacity ` ` ` `double` `tank_cap = ` `3.14` `* r * r * H; ` ` ` ` ` `// volume of water in tank ` ` ` `double` `water_vol = ` `3.14` `* r * r * h; ` ` ` ` ` `// volume of n balls ` ` ` `double` `balls_vol = N * (` `4` `/ ` `3` `) * ` `3.14` `* R * R * R; ` ` ` ` ` `// total volume of water ` ` ` `// and n dipped balls ` ` ` `double` `vol = water_vol + balls_vol; ` ` ` ` ` `/* condition to check if tank is in ` ` ` `overflow state or not */` ` ` `if` `(vol > tank_cap) { ` ` ` `System.out.println(` `"Overflow"` `); ` ` ` `} ` ` ` `else` `{ ` ` ` `System.out.println(` `"Not in overflow state"` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `/* Driver program to test above function */` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `// giving dimensions ` ` ` `int` `H = ` `10` `, r = ` `5` `, h = ` `5` `, ` ` ` `N = ` `2` `, R = ` `2` `; ` ` ` ` ` `// calling function ` ` ` `overflow(H, r, h, N, R); ` ` ` `} ` `} ` ` ` `// This code is contributed by Arnav Kr. Mandal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python code to check if water tank ` `# overflows when n solid balls are ` `# dipped in the water tank ` ` ` `# function to find if tak will ` `# overflow or not ` `def` `overflow( H, r, h, N, R ): ` ` ` ` ` `# cylinder capacity ` ` ` `tank_cap ` `=` `3.14` `*` `r ` `*` `r ` `*` `H ` ` ` ` ` `# volume of water in tank ` ` ` `water_vol ` `=` `3.14` `*` `r ` `*` `r ` `*` `h ` ` ` ` ` `# volume of n balls ` ` ` `balls_vol ` `=` `N ` `*` `(` `4` `/` `3` `) ` `*` `3.14` `*` `R ` `*` `R ` `*` `R ` ` ` ` ` `# total volume of water ` ` ` `# and n dipped balls ` ` ` `vol ` `=` `water_vol ` `+` `balls_vol ` ` ` ` ` `# condition to check if tank is in ` ` ` `# overflow state or not ` ` ` `if` `vol > tank_cap: ` ` ` `print` `(` `"Overflow"` `) ` ` ` `else` `: ` ` ` `print` `(` `"Not in overflow state"` `) ` ` ` `# Driver code ` ` ` `# giving dimensions ` `H ` `=` `10` `r ` `=` `5` `h ` `=` `5` `N ` `=` `2` `R ` `=` `2` ` ` `# calling function ` `overflow (H, r, h, N, R) ` ` ` `# This code is contributed by "Sharad_Bhardwaj". ` |

*chevron_right*

*filter_none*

## C#

`// C# Code for Program to check if ` `// water tank overflows ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// function to find if tak will ` ` ` `// overflow or not ` ` ` `static` `void` `overflow(` `int` `H, ` `int` `r, ` `int` `h, ` ` ` `int` `N, ` `int` `R) ` ` ` `{ ` ` ` `// cylinder capacity ` ` ` `double` `tank_cap = 3.14 * r * r * H; ` ` ` ` ` `// volume of water in tank ` ` ` `double` `water_vol = 3.14 * r * r * h; ` ` ` ` ` `// volume of n balls ` ` ` `double` `balls_vol = N * (4 / 3) * 3.14 * R * R * R; ` ` ` ` ` `// total volume of water ` ` ` `// and n dipped balls ` ` ` `double` `vol = water_vol + balls_vol; ` ` ` ` ` `/* condition to check if tank is in ` ` ` `overflow state or not */` ` ` `if` `(vol > tank_cap) { ` ` ` `Console.WriteLine(` `"Overflow"` `); ` ` ` `} ` ` ` `else` `{ ` ` ` `Console.WriteLine(` `"Not in overflow state"` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `/* Driver program to test above function */` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `// giving dimensions ` ` ` `int` `H = 10, r = 5, h = 5, ` ` ` `N = 2, R = 2; ` ` ` ` ` `// calling function ` ` ` `overflow(H, r, h, N, R); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_M. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP Program to check if water tank ` `// overflows when n solid balls are ` `// dipped in the water tank ` ` ` `// function to find if tank ` `// will overflow or not ` ` ` `function` `overflow(` `$H` `, ` `$r` `, ` `$h` `, ` ` ` `$N` `, ` `$R` `) ` `{ ` ` ` `// cylinder capacity ` ` ` `$tank_cap` `= 3.14 * ` `$r` `* ` ` ` `$r` `* ` `$H` `; ` ` ` ` ` `// volume of water in tank ` ` ` `$water_vol` `= 3.14 * ` `$r` `* ` ` ` `$r` `* ` `$h` `; ` ` ` ` ` `// volume of n balls ` ` ` `$balls_vol` `= ` `$N` `* (4/3) * ` ` ` `3.14 * ` `$R` `* ` ` ` `$R` `* ` `$R` `; ` ` ` ` ` `// total volume of water ` ` ` `// and n dipped balls ` ` ` `$vol` `= ` `$water_vol` `+ ` `$balls_vol` `; ` ` ` ` ` `// condition to check if tank ` ` ` `// is in overflow state or not ` ` ` `if` `(` `$vol` `> ` `$tank_cap` `) ` ` ` `{ ` ` ` `echo` `"Overflow"` `, ` `"\n"` `; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `echo` `"Not in overflow state"` `, ` `"\n"` `; ` ` ` ` ` `} ` `} ` ` ` `// Driver Code ` `// giving dimensions ` `$H` `= 10; ` `$r` `= 5; ` `$h` `= 5; ` `$N` `= 2; ` `$R` `= 2; ` ` ` `// calling function ` `overflow (` `$H` `, ` `$r` `, ` `$h` `, ` `$N` `, ` `$R` `); ` ` ` `// This code is contributed by aj_36 ` `?> ` |

*chevron_right*

*filter_none*

Output :

Not in overflow state

## Recommended Posts:

- Find amount of water wasted after filling the tank
- Program to check if tank will overflow, underflow or filled in given time
- Program to find amount of water in a given glass
- Container with Most Water
- Measuring 6L water from 4L and 9L buckets
- Measure one litre using two vessels and infinite water supply
- Ways to arrange Balls such that adjacent balls are of different types
- Ways to choose balls such that at least one ball is chosen
- Program to check similarity of given two triangles
- Program to check Strong Number
- Program to check congruency of two triangles
- C Program to Check if count of divisors is even or odd
- Program to check Plus Perfect Number
- Program to check if three points are collinear
- Program to check Involutory Matrix

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.