Program to check if tank will overflow, underflow or filled in given time

Given a tank with definite height and radius and the flow of water available to fill the tank. Determine whether the tank will overflow or not in a given amount of time.
Note: The flow of water will be available in volume per minute.

Examples:

       --r=5--
   ----------   ^
   |         |  |
   |         |  |
   |         |  h = 10
   |         |  |
   ----------   ^
        
   rate_of_flow = 10

Input : given_time = 10.0
Output : Underflow

Input : given_time = 100.0
Output : Overflow



Approach:
Volume of a cylindrical tank is (22/7) * radius * radius * height. First, find out the amount of time required to completely fill the tank then compare it with the given time. If the given time is greater than required time, it will result in an overflow condition. If the given time is less than the required time then it will result in an underflow condition otherwise the tank is filled.

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to check if Tank will
// overflow or not in given time
#include <bits/stdc++.h>
using namespace std;
  
// function to calculate the volume of tank
float volume(int radius, int height) 
{
    return ((22 / 7) * radius * radius * height);
}
  
// function to print overflow / filled / 
// underflow accordingly
void check_and_print(float required_time, 
                       float given_time) 
{
    if (required_time < given_time)
        cout << "Overflow";
    else if (required_time > given_time)
        cout << "Underflow";
    else
        cout << "Filled";
}
  
// driver function
int main()
{
    int radius = 5, // radius of the tank
        height = 10, // height of the tank
        rate_of_flow = 10; // rate of flow of water
          
    float given_time = 70.0; // time given
      
    // calculate the required time
    float required_time = volume(radius, height) /
                                    rate_of_flow;
      
    // printing the result
    check_and_print(required_time, given_time); 
    return 0;

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check if Tank will
// overflow or not in given time
  
class Number
{
    // function to calculate the volume of tank
    public static float volume(int radius, int height) 
    {
        return ((22 / 7) * radius * radius * height);
    }
  
    // function to print overflow / filled / 
    // underflow accordingly
    public static void check_and_print(double required_time, 
                                       double given_time) 
    {
        if (required_time < given_time)
            System.out.print( "Overflow" );
        else if (required_time > given_time)
            System.out.print( "Underflow" );
        else
            System.out.print( "Filled" );
    }
      
    // driver code
    public static void main(String[] args)
    {
        int radius = 5, // radius of the tank
        height = 10, // height of the tank
        rate_of_flow = 10; // rate of flow of water
          
        double given_time = 70.0; // time given
      
        // calculate the required time
        double required_time = volume(radius, height) /
                                    rate_of_flow;
      
        // printing the result
        check_and_print(required_time, given_time); 
    }
}
  
// This code is contributed by rishabh_jain

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to check if Tank will
# overflow or not in given time
  
# function to calculate the volume of tank
def volume(radius, height):
    return ((22 / 7) * radius * radius * height)
      
# function to print overflow / filled / 
# underflow accordingly
def check_and_print( required_time, given_time):
      
    if required_time < given_time:
        print( "Overflow")
    elif required_time > given_time:
        print("Underflow")
    else:
        print("Filled")
  
# driver code
radius = 5 # radius of the tank
height = 10 # height of the tank
rate_of_flow = 10 # rate of flow of water
          
given_time = 70.0 # time given
      
# calculate the required time
required_time = volume(radius, height) /rate_of_flow
      
# printing the result
check_and_print(required_time, given_time)
  
# This code is contributed by "Sharad_Bhardwaj".

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check if Tank will
// overflow or not in given time
using System;
  
class Number {
      
    // function to calculate the volume of tank
    public static float volume(int radius, int height)
    {
        return ((22 / 7) * radius * radius * height);
    }
  
    // function to print overflow / filled /
    // underflow accordingly
    public static void check_and_print(double required_time,
                                          double given_time)
    {
        if (required_time < given_time)
            Console.WriteLine("Overflow");
        else if (required_time > given_time)
            Console.WriteLine("Underflow");
        else
            Console.WriteLine("Filled");
    }
  
    // driver code
    public static void Main()
    {
        int radius = 5, // radius of the tank
            height = 10, // height of the tank
            rate_of_flow = 10; // rate of flow of water
  
        double given_time = 70.0; // time given
  
        // calculate the required time
        double required_time = volume(radius, height) / rate_of_flow;
  
        // printing the result
        check_and_print(required_time, given_time);
    }
}
  
// This code is contributed by vt_m

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to check if Tank will
// overflow or not in given time
  
// function to calculate
// the volume of tank
function volume($radius, $height
{
    return ((22 / 7) * $radius
              $radius * $height);
}
  
// function to print overflow 
// / filled / underflow accordingly
function check_and_print($required_time
                         $given_time
{
    if ($required_time < $given_time)
        echo("Overflow");
    else if ($required_time > $given_time)
        echo("Underflow");
    else
        echo("Filled");
}
  
// Driver code
  
// radius of the tank
$radius = 5; 
  
// height of the tank
$height = 10; 
  
// rate of flow of water
$rate_of_flow = 10; 
  
// time given        
$given_time = 70.0; 
      
// calculate the required time
$required_time = volume($radius, $height) /
                            $rate_of_flow;
      
// printing the result
check_and_print($required_time, $given_time); 
  
// This code is contributed by Ajit.
?>

chevron_right



Output:

Underflow


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : jit_t