Program to find the Eccentricity of a Hyperbola
Last Updated :
13 May, 2021
Given two integers A and B, representing the length of the semi-major and semi-minor axis of a Hyperbola of the equation (X2 / A2) – (Y2 / B2) = 1, the task is to calculate the eccentricity of the given hyperbola.
Examples:
Input: A = 3, B = 2
Output: 1.20185
Explanation:
The eccentricity of the given hyperbola is 1.20185.
Input: A = 6, B = 3
Output: 1.11803
Approach: The given problem can be solved by using the formula to find the eccentricity of an ellipse.
- The length of the semi-major axis is A.
- The length of the semi-minor axis is B.
- Therefore, the eccentricity of the ellipse is given by where A > B
Therefore, the idea is to print the value of as the eccentricity of the ellipse.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
double eccHyperbola( double A, double B)
{
double r = ( double )B * B / A * A;
r += 1;
return sqrt (r);
}
int main()
{
double A = 3.0, B = 2.0;
cout << eccHyperbola(A, B);
return 0;
}
|
Java
import java.util.*;
class GFG{
static double eccHyperbola( double A, double B)
{
double r = ( double )B * B / A * A;
r += 1 ;
return Math.sqrt(r);
}
public static void main(String[] args)
{
double A = 3.0 , B = 2.0 ;
System.out.print(eccHyperbola(A, B));
}
}
|
Python3
import math
def eccHyperbola(A, B):
r = B * B / A * A
r + = 1
return math.sqrt(r)
if __name__ = = "__main__" :
A = 3.0
B = 2.0
print (eccHyperbola(A, B))
|
C#
using System;
class GFG{
static double eccHyperbola( double A, double B)
{
double r = ( double )B * B / A * A;
r += 1;
return Math.Sqrt(r);
}
public static void Main(String[] args)
{
double A = 3.0, B = 2.0;
Console.Write(eccHyperbola(A, B));
}
}
|
Javascript
<script>
function eccHyperbola(A, B)
{
let r = B * B / A * A;
r += 1;
return Math.sqrt(r);
}
let A = 3.0;
let B = 2.0;
document.write(eccHyperbola(A, B));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...