# Hackerrank Solution: Find Angle MBC in Python [4 Methods]

Topics we will cover

## Question: Find Angle MBC - Hacker Rank (Python)

ABC is a right triangle, 90o at B.
Therefore, angle ABC = 90o.
Point M is the midpoint of hypotenuse AC.

You are given the lengths AB and BC.

Input format:

The first line contains the length of side AB.
The second line contains the length of side BC.

Constraints:

• 0 < AB <= 100
• 0 < BC <= 100
• Lengths AB and BC are natural numbers.

Output format:

Output angle MBC in degrees.

Note: Round the angle to the nearest integer.

Examples:
If angle is 56.5000001°, then output 57°.
If angle is 56.5000000°, then output 57°.
If angle is 56.4999999°, then output 56°.

Sample input:

``````10
10``````

Output:

``45``

## Possible solutions

As we will be provided by the sides and then we have to find the angle. For this question, we can come up with multiple solutions.

• Using math module
• Using math module: Alternative solution
• Using math module with fewer lines of code
• Alternative solution

### Solution-1: Using math module

The math module is a standard module in Python and is always available. To use mathematical functions under this module, you have to import the module using import math. Let us import the math module and find the angle using the given sides of the triangle.

``````# importing the module
import math

# taking the input from user
ab = float(input())
bc = float(input())

# finding the distance
ac = math.sqrt((ab*ab)+(bc*bc))
bm = ac / 2.0
mc = bm

# equalizing the sides
b = mc
c = bm
a = bc

# where b=c
# finding the angle in radian

# converting the radian to degree
angel_b_degree = int(round((180 * angel_b_radian) / math.pi))

# printing with degree
print(angel_b_degree,'\u00B0',sep='')``````

Input:

``````10
10``````

Output:

`45°`

As you can see, we get the 45-degree angle as required.

### Solution-2: Using a math module: Alternative solution

Now, we will transform the above code into fewer lines as an alternative solution shown below:

``````# importing the math module
import math

# taking input of the sides
ab=int(input())
bc=int(input())

# finding the distance
ca=math.hypot(ab,bc)
mc=ca/2

# finding the angle
bca=math.asin(1*ab/ca)
bm=math.sqrt((bc**2+mc**2)-(2*bc*mc*math.cos(bca)))
mbc=math.asin(math.sin(bca)*mc/bm)

# printing the angle
print(int(round(math.degrees(mbc),0)),'\u00B0',sep='')``````

Input:

``````10
10``````

Output:

`45°`

As you can see, we get 45 degrees which was required in the sample.

### Solution-3: Using Math module with fewer lines

We can even reduce the number of lines to make our code more optimized and readable.

``````# importing math
import math

# taking inputs
a = int(input())
b = int(input())

# finding distace and angle
M = math.sqrt(a**2+b**2)
theta = math.acos(b/M )

# printing the angle
print(int(round(math.degrees(theta),0)),'\u00B0',sep='')``````

Input:

``````10
10``````

Output:

`45°`

As you can see, we were able to get the same result using fewer lines of code.

### Solution-4: Alternative Solution

Now, we will use another simple method to calculate the angle.

``````# importing math
import math

# taking input from user
AB,BC=int(input()),int(input())

# calculating distance
hype=math.hypot(AB,BC)

# calculating the angle
res=round(math.degrees(math.acos(BC/hype)))

# the 176 represents the degree symbol
degree=chr(176)
print(res,degree, sep='')``````

Input:

``````10
10``````

Output:

`45°`

The char(176) in the code represents the degree sign.

## Summary

As we were supposed to find the angle of a triangle when the sides were given. In this short article, we come up with multiple solutions to solve the FIND ANGLE question from Hacker Rank.

## References

Related Keywords: hackerrank find angle mbc solution in python, hackerrank find angle mbc solution in python3, 1find angle mbc hackerrank solution

Views: 159

Bashir Alam

He is a Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in OCR, text extraction, data preprocessing, and predictive models. You can reach out to him on his Linkedin or check his projects on GitHub page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com