📜  求椭圆偏心率的程序

📅  最后修改于: 2021-10-23 08:57:09             🧑  作者: Mango

给定两个正整数AB ,表示方程椭圆的长半轴和半短轴的长度\frac{x^2}{A^2} + \frac{y^2}{B^2} = 1         ,任务是找到给定椭圆的偏心率。

例子:

方法:给定的问题可以根据以下公式解决,以计算椭圆的偏心率,其由下式给出:

因此,打印值\sqrt(1 - \frac{B^2}{A^2})         作为椭圆的偏心率。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the
// eccentricity of ellipse
void findEccentricity(double A,
                      double B)
{
    // Store the squares of length of
    // semi-major and semi-minor axis
    double semiMajor = A * A;
    double semiMinor = B * B;
 
    // Calculate the eccentricity
    double ans = sqrt(1 - semiMinor / semiMajor);
 
    // Print the result
    cout << fixed << setprecision(2)
         << ans;
}
 
// Driver Code
int main()
{
    double A = 12, B = 9;
    findEccentricity(A, B);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the
// eccentricity of ellipse
static void findEccentricity(double A, double B)
{
    // Store the squares of length of
    // semi-major and semi-minor axis
    double semiMajor = A * A;
    double semiMinor = B * B;
     
    // Calculate the eccentricity
    double ans = Math.sqrt(1 - semiMinor / semiMajor);
 
    // Print the result
    System.out.format("%.2f", ans);
}
 
// Driver Code
public static void main(String arr[])
{
    double A = 12, B = 9;
    findEccentricity(A, B);
}
}
 
// This code is contributed by kirti


Python3
# Python3 program for the above approach
import math
 
# Function to find the
# eccentricity of ellipse
def findEccentricity(A, B):
     
    # Store the squares of length of
    # semi-major and semi-minor axis
    semiMajor = A * A
    semiMinor = B * B
 
    # Calculate the eccentricity
    ans = math.sqrt(1 - semiMinor / semiMajor)
 
    # Print the result
    print('%.2f' % ans)
 
# Driver Code
if __name__ == "__main__":
 
    A = 12
    B = 9
     
    findEccentricity(A, B)
     
# This code is contributed by ukasp


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the
// eccentricity of ellipse
static void findEccentricity(double A, double B)
{
     
    // Store the squares of length of
    // semi-major and semi-minor axis
    double semiMajor = A * A;
    double semiMinor = B * B;
      
    // Calculate the eccentricity
    double ans = Math.Sqrt(1 - semiMinor / semiMajor);
  
    // Print the result
    Console.Write(Math.Round(ans, 2));
}
 
// Driver code
static void Main()
{
    double A = 12, B = 9;
     
    findEccentricity(A, B);
}
}
 
// This code is contributed by code_hunt


Javascript


输出:
0.66

时间复杂度: O(1)
辅助空间: O(1)