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++
#include <bits/stdc++.h>
using namespace std;
float volume( int radius, int height)
{
return ((22 / 7) * radius * radius * height);
}
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" ;
}
int main()
{
int radius = 5,
height = 10,
rate_of_flow = 10;
float given_time = 70.0;
float required_time = volume(radius, height) /
rate_of_flow;
check_and_print(required_time, given_time);
return 0;
}
|
Java
class Number
{
public static float volume( int radius, int height)
{
return (( 22 / 7 ) * radius * radius * height);
}
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" );
}
public static void main(String[] args)
{
int radius = 5 ,
height = 10 ,
rate_of_flow = 10 ;
double given_time = 70.0 ;
double required_time = volume(radius, height) /
rate_of_flow;
check_and_print(required_time, given_time);
}
}
|
Python3
def volume(radius, height):
return (( 22 / 7 ) * radius * radius * height)
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" )
radius = 5
height = 10
rate_of_flow = 10
given_time = 70.0
required_time = volume(radius, height) / rate_of_flow
check_and_print(required_time, given_time)
|
C#
using System;
class Number {
public static float volume( int radius, int height)
{
return ((22 / 7) * radius * radius * height);
}
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" );
}
public static void Main()
{
int radius = 5,
height = 10,
rate_of_flow = 10;
double given_time = 70.0;
double required_time = volume(radius, height) / rate_of_flow;
check_and_print(required_time, given_time);
}
}
|
PHP
<?php
function volume( $radius , $height )
{
return ((22 / 7) * $radius *
$radius * $height );
}
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" );
}
$radius = 5;
$height = 10;
$rate_of_flow = 10;
$given_time = 70.0;
$required_time = volume( $radius , $height ) /
$rate_of_flow ;
check_and_print( $required_time , $given_time );
?>
|
Javascript
<script>
function volume(radius, height)
{
return ((22 / 7) * radius * radius * height);
}
function check_and_print(required_time,
given_time)
{
if (required_time < given_time)
document.write( "Overflow" );
else if (required_time > given_time)
document.write( "Underflow" );
else
document.write( "Filled" );
}
let radius = 5,
height = 10,
rate_of_flow = 10;
let given_time = 70.0;
let required_time = volume(radius, height) /
rate_of_flow;
check_and_print(required_time, given_time);
</script>
|
Output:
Underflow
Time Complexity: O(1)
Auxiliary Space: O(1)