Skip to content
Related Articles

Related Articles

Program to find Length of Latus Rectum of an Ellipse
  • Last Updated : 07 Apr, 2021

Given two integers A and B, representing the length of semi-major and semi-minor axis of an Ellipse with general equation (x2 / A2) + (y2 / B2) = 1, the task is to find the length of the latus rectum of the ellipse

Examples:

Input: A = 3, B = 2
Output: 2.66666

Input: A = 6, B = 3
Output: 3

Approach: The given problem can be solved based on the following observations: 



  • The Latus Rectum of an Ellipse is the focal chord perpendicular to the major axis whose length is equal to:
     \frac{(length\ of\  minor\ axis)^2}{(length\ of\ major\ axis)}

Ellipse

  • Length of major axis is 2A.
  • Length of minor axis is 2B.
  • Therefore, the length of the latus rectum is:
     d_{LL'}=2\frac{B^2}{A}

Follow the steps below to solve the given problem:

  • Initialize two variables, say major and minor, to store the length of the major-axis (= 2A) and the length of the minor-axis (= 2B) of the Ellipse respectively.
  • Calculate the square of minor and divide it with major. Store the result in a double variable, say latus_rectum.
  • Print the value of latus_rectum as the final result. 

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <iostream>
using namespace std;
 
// Function to calculate the length
// of the latus rectum of an ellipse
double lengthOfLatusRectum(double A,
                           double B)
{
    // Length of major axis
    double major = 2.0 * A;
   
    // Length of minor axis
    double minor = 2.0 * B;
   
    // Length of the latus rectum
    double latus_rectum = (minor*minor)/major;
   
    return latus_rectum;
}
 
// Driver Code
int main()
{
    // Given lengths of semi-major
  // and semi-minor axis
    double A = 3.0, B = 2.0;
   
    // Function call to calculate length
    // of the latus rectum of a ellipse
    cout << lengthOfLatusRectum(A, B);
    return 0;
}

Java




// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to calculate the length
// of the latus rectum of an ellipse
static double lengthOfLatusRectum(double A,
                                  double B)
{
     
    // Length of major axis
    double major = 2.0 * A;
     
    // Length of minor axis
    double minor = 2.0 * B;
     
    // Length of the latus rectum
    double latus_rectum = (minor * minor) / major;
     
    return latus_rectum;
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given lengths of semi-major
    // and semi-minor axis
    double A = 3.0, B = 2.0;
 
    // Function call to calculate length
    // of the latus rectum of a ellipse
    System.out.print(lengthOfLatusRectum(A, B));
}
}
 
// This code is contributed by susmitakundugoaldanga

Python3




# Python3 program for the above approach
 
# Function to calculate the length
# of the latus rectum of an ellipse
def lengthOfLatusRectum(A, B):
   
    # Length of major axis
    major = 2.0 * A
 
    # Length of minor axis
    minor = 2.0 * B
 
    # Length of the latus rectum
    latus_rectum = (minor*minor)/major
    return latus_rectum
 
# Driver Code
if __name__ == "__main__":
 
    # Given lengths of semi-major
        # and semi-minor axis
    A = 3.0
    B = 2.0
 
    # Function call to calculate length
    # of the latus rectum of a ellipse
    print('%.5f' % lengthOfLatusRectum(A, B))
 
    # This code is contributed by ukasp.

C#




// C# program for the above approach
using System;
 
class GFG
{
 
  // Function to calculate the length
  // of the latus rectum of an ellipse
  static double lengthOfLatusRectum(double A,
                                    double B)
  {
    // Length of major axis
    double major = 2.0 * A;
 
    // Length of minor axis
    double minor = 2.0 * B;
 
    // Length of the latus rectum
    double latus_rectum = (minor*minor)/major;
 
    return latus_rectum;
  }
 
  // Driver Code
  public static void Main()
  {
 
    // Given lengths of semi-major
    // and semi-minor axis
    double A = 3.0, B = 2.0;
 
    // Function call to calculate length
    // of the latus rectum of a ellipse
    Console.WriteLine(lengthOfLatusRectum(A, B));
  }
}
 
// This code is contributed by souravghosh0416.

Javascript




<script>
 
// Javascript program for the above approach
 
// Function to calculate the length
// of the latus rectum of an ellipse
function lengthOfLatusRectum(A, B)
{
     
    // Length of major axis
    var major = 2.0 * A;
     
    // Length of minor axis
    var minor = 2.0 * B;
     
    // Length of the latus rectum
    var latus_rectum = (minor * minor) / major;
     
    return latus_rectum;
}
 
// Driver code
 
// Given lengths of semi-major
// and semi-minor axis
var A = 3.0, B = 2.0;
 
document.write(lengthOfLatusRectum(A, B));
 
// This code is contributed by Ankita saini
    
</script>
Output: 
2.66667

 

Time Complexity: O(1)
Auxiliary Space: O(1) 

 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :