Open In App

Program to find Length of Latus Rectum of an Ellipse

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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) 

Using the formula :

Approach:

The length of the Latus Rectum of an Ellipse can be calculated using the formula: L = 2b^2/a, where a and b are the lengths of the major and minor axis of the ellipse, respectively.

Define a function latus_rectum that takes two arguments a and b.
Inside the function, calculate the length of the Latus Rectum using the formula 2 * b ** 2 / a and return the result.
Call the function twice with different values of a and b.
Print the results using formatted strings to display the inputs and outputs with appropriate decimal places.

C++
#include <iostream>
#include <iomanip> // For setting the precision of the output

// Function to calculate the length of Latus Rectum
double latusRectum(double a, double b) {
    return (2 * b * b) / a;
}

int main() {
    // Example inputs
    double a1 = 3, b1 = 2;
    double a2 = 6, b2 = 3;

    // Calculate the length of Latus Rectum for the first ellipse
    double l1 = latusRectum(a1, b1);

    // Display the result with formatted string
    std::cout << "The length of the Latus Rectum of the ellipse with a = " 
         << a1 << " and b = " << b1
              << " is " << std::fixed << std::setprecision(5) << l1 << std::endl;

    // Calculate the length of Latus Rectum for the second ellipse
    double l2 = latusRectum(a2, b2);

    // Display the result with formatted string
    std::cout << "The length of the Latus Rectum of the ellipse with a = " 
              << a2 << " and b = " << b2
              << " is " << std::fixed << std::setprecision(5) << l2 << std::endl;

    return 0;
}
Java
public class Main {
    // Function to calculate the length of Latus Rectum
    static double latusRectum(double a, double b) {
        return (2 * b * b) / a;
    }

    public static void main(String[] args) {
        // Example inputs
        double a1 = 3, b1 = 2;
        double a2 = 6, b2 = 3;

        // Calculate the length of Latus Rectum for the first ellipse
        double l1 = latusRectum(a1, b1);

        // Display the result with formatted string
        System.out.println("The length of the Latus Rectum of the ellipse with a = " + a1 +
                " and b = " + b1 + " is " + String.format("%.5f", l1));

        // Calculate the length of Latus Rectum for the second ellipse
        double l2 = latusRectum(a2, b2);

        // Display the result with formatted string
        System.out.println("The length of the Latus Rectum of the ellipse with a = " + a2 +
                " and b = " + b2 + " is " + String.format("%.5f", l2));
    }
}

// This code is contributed by shivamgupta0987654321
Python3
def latus_rectum(a, b):
    return 2 * b ** 2 / a

# Example inputs
a1, b1 = 3, 2
a2, b2 = 6, 3

# Calculate the length of Latus Rectum for the first ellipse
l1 = latus_rectum(a1, b1)

# Display the result with formatted string
print(f"The length of the Latus Rectum of the ellipse with a = {a1} and b = {b1} is {l1:.5f}")

# Calculate the length of Latus Rectum for the second ellipse
l2 = latus_rectum(a2, b2)

# Display the result with formatted string
print(f"The length of the Latus Rectum of the ellipse with a = {a2} and b = {b2} is {l2:.5f}")
C#
using System;

class Program
{
    // Function to calculate the length of Latus Rectum
    static double LatusRectum(double a, double b)
    {
        return (2 * b * b) / a;
    }

    static void Main()
    {
        // Example inputs
        double a1 = 3, b1 = 2;
        double a2 = 6, b2 = 3;

        // Calculate the length of Latus Rectum for the first ellipse
        double l1 = LatusRectum(a1, b1);

        // Display the result with formatted string
        Console.WriteLine($"The length of the Latus Rectum of the ellipse with a = {a1} and b = {b1} is {l1:F5}");

        // Calculate the length of Latus Rectum for the second ellipse
        double l2 = LatusRectum(a2, b2);

        // Display the result with formatted string
        Console.WriteLine($"The length of the Latus Rectum of the ellipse with a = {a2} and b = {b2} is {l2:F5}");
    }
}
// This code is contributed by shivamgupta310570
JavaScript
// Function to calculate the length of Latus Rectum
function latusRectum(a, b) {
    return (2 * b * b) / a;
}

// Example inputs
let a1 = 3, b1 = 2;
let a2 = 6, b2 = 3;

// Calculate the length of Latus Rectum for the first ellipse
let l1 = latusRectum(a1, b1);

// Display the result with formatted string
console.log(`The length of the Latus Rectum of the ellipse with a = ${a1} and b = ${b1} is ${l1.toFixed(5)}`);

// Calculate the length of Latus Rectum for the second ellipse
let l2 = latusRectum(a2, b2);

// Display the result with formatted string
console.log(`The length of the Latus Rectum of the ellipse with a = ${a2} and b = ${b2} is ${l2.toFixed(5)}`);

Output
The length of the Latus Rectum of the ellipse with a = 3 and b = 2 is 2.66667
The length of the Latus Rectum of the ellipse with a = 6 and b = 3 is 3.00000

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


Similar Reads