Find the maximum angle at which we can tilt the bottle without spilling any water

Given a water bottle with the shape of a rectangular prism whose base is a square of side x cm and height y cm, the task is to find the maximum angle at which we can tilt the bottle without spilling any water, when z cubic cm of water is poured into the bottle, and gradually tilt the bottle around one of the sides of the base.

Examples:

Input: x = 2, y = 2, z = 4
Output: 45.00000
Explanation:
This bottle has a cubic shape,
and it is half-full. 
The water gets spilt when we tilt
the bottle more than 45 degrees.

Input: x = 12, y = 21, z = 10
Output: 89.78346

Approach:

First of all, we need to know that the surface of the water is always parallel to the ground. So if the bottle tilts the water remain parallel to the ground.

Here we have 2 cases:-



  1. When the water is less than half of the total volume of the bottle, Then we consider the lower right triangle formed by water inside the bottle and with the help of tangent inverse calculate the required angle.
  2. When the water is greater than or equal to the total volume of the bottle, then we consider the upper right triangle formed by empty space inside the bottle. Now we can calculate the angle easily.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the maximum angle
// at which we can tilt the bottle
// without spilling any water
  
#include <bits/stdc++.h>
using namespace std;
  
float find_angle(int x, int y, int z)
{
  
    // Now we have the volume
    // of rectangular prism a*a*b
    int volume = x * x * y;
  
    float ans = 0;
  
    // Now we have 2 cases!
    if (z < volume / 2) {
  
        float d = (x * y * y) / (2.0 * z);
  
        // Taking the tangent inverse of value d
        // As we want to take out the required angle
        ans = atan(d);
    }
    else {
  
        z = volume - z;
        float d = (2 * z) / (float)(x * x * x);
  
        // Taking the tangent inverse of value d
        // As we want to take out the required angle
        ans = atan(d);
    }
  
    // As of now the angle is in radian.
    // So we have to convert it in degree.
    ans = (ans * 180) / 3.14159265;
  
    return ans;
}
  
int main()
{
    // Enter the Base square side length
    int x = 12;
  
    // Enter the Height of rectangular prism
    int y = 21;
  
    // Enter the Base square side length
    int z = 10;
  
    cout << find_angle(x, y, z) << endl;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the maximum angle
// at which we can tilt the bottle
// without spilling any water
class GFG 
{
    static float find_angle(int x,
                     int y, int z)
    {
  
        // Now we have the volume
        // of rectangular prism a*a*b
        int volume = x * x * y;
  
        float ans = 0;
  
        // Now we have 2 cases!
        if (z < volume / 2
        {
            float d = (float) ((x * y * y) / (2.0 * z));
  
            // Taking the tangent inverse of value d
            // As we want to take out the required angle
            ans = (float) Math.atan(d);
        
        else
        {
            z = volume - z;
            float d = (2 * z) / (float) (x * x * x);
  
            // Taking the tangent inverse of value d
            // As we want to take out the required angle
            ans = (float) Math.atan(d);
        }
  
        // As of now the angle is in radian.
        // So we have to convert it in degree.
        ans = (float) ((ans * 180) / 3.14159265);
  
        return ans;
    }
  
    // Driver Code
    public static void main(String[] args) 
    {
        // Enter the Base square side length
        int x = 12;
  
        // Enter the Height of rectangular prism
        int y = 21;
  
        // Enter the Base square side length
        int z = 10;
  
        System.out.print(find_angle(x, y, z) + "\n");
    }
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the maximum angle 
# at which we can tilt the bottle 
# without spilling any water 
from math import *
  
def find_angle(x, y, z) :
  
    # Now we have the volume 
    # of rectangular prism a*a*b 
    volume = x * x * y; 
  
    ans = 0
  
    # Now we have 2 cases! 
    if (z < volume // 2) :
  
        d = (x * y * y) / (2.0 * z); 
  
        # Taking the tangent inverse of value d 
        # As we want to take out the required angle 
        ans = atan(d); 
  
    else :
  
        z = volume - z; 
        d = (2 * z) / (float)(x * x * x); 
  
        # Taking the tangent inverse of value d 
        # As we want to take out the required angle 
        ans = atan(d); 
      
    # As of now the angle is in radian. 
    # So we have to convert it in degree. 
    ans = (ans * 180) / 3.14159265
  
    return round(ans, 4); 
  
# Driver Code
if __name__ == "__main__"
  
    # Enter the Base square side length 
    x = 12
  
    # Enter the Height of rectangular prism 
    y = 21
  
    # Enter the Base square side length 
    z = 10
  
    print(find_angle(x, y, z)); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the maximum angle
// at which we can tilt the bottle
// without spilling any water
using System;
  
class GFG 
{
    static float find_angle(int x,
                     int y, int z)
    {
  
        // Now we have the volume
        // of rectangular prism a*a*b
        int volume = x * x * y;
  
        float ans = 0;
  
        // Now we have 2 cases!
        if (z < volume / 2) 
        {
            float d = (float) ((x * y * y) / (2.0 * z));
  
            // Taking the tangent inverse of value d
            // As we want to take out the required angle
            ans = (float) Math.Atan(d);
        
        else
        {
            z = volume - z;
            float d = (2 * z) / (float) (x * x * x);
  
            // Taking the tangent inverse of value d
            // As we want to take out the required angle
            ans = (float) Math.Atan(d);
        }
  
        // As of now the angle is in radian.
        // So we have to convert it in degree.
        ans = (float) ((ans * 180) / 3.14159265);
  
        return ans;
    }
  
    // Driver Code
    public static void Main(String[] args) 
    {
        // Enter the Base square side length
        int x = 12;
  
        // Enter the Height of rectangular prism
        int y = 21;
  
        // Enter the Base square side length
        int z = 10;
  
        Console.Write(find_angle(x, y, z) + "\n");
    }
}
  
// This code is contributed by Rajput-Ji

chevron_right


Output:

89.7835

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




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.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.