Program to find Length of Latus Rectum of an Ellipse
Last Updated : 23 Jul, 2025
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 minorand 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>usingnamespacestd;// Function to calculate the length// of the latus rectum of an ellipsedoublelengthOfLatusRectum(doubleA,doubleB){// Length of major axisdoublemajor=2.0*A;// Length of minor axisdoubleminor=2.0*B;// Length of the latus rectumdoublelatus_rectum=(minor*minor)/major;returnlatus_rectum;}// Driver Codeintmain(){// Given lengths of semi-major// and semi-minor axisdoubleA=3.0,B=2.0;// Function call to calculate length // of the latus rectum of a ellipsecout<<lengthOfLatusRectum(A,B);return0;}
Java
// Java program for the above approachimportjava.util.*;classGFG{// Function to calculate the length// of the latus rectum of an ellipsestaticdoublelengthOfLatusRectum(doubleA,doubleB){// Length of major axisdoublemajor=2.0*A;// Length of minor axisdoubleminor=2.0*B;// Length of the latus rectumdoublelatus_rectum=(minor*minor)/major;returnlatus_rectum;}// Driver codepublicstaticvoidmain(String[]args){// Given lengths of semi-major// and semi-minor axisdoubleA=3.0,B=2.0;// Function call to calculate length // of the latus rectum of a ellipseSystem.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 ellipsedeflengthOfLatusRectum(A,B):# Length of major axismajor=2.0*A# Length of minor axisminor=2.0*B# Length of the latus rectumlatus_rectum=(minor*minor)/majorreturnlatus_rectum# Driver Codeif__name__=="__main__":# Given lengths of semi-major# and semi-minor axisA=3.0B=2.0# Function call to calculate length# of the latus rectum of a ellipseprint('%.5f'%lengthOfLatusRectum(A,B))# This code is contributed by ukasp.
C#
// C# program for the above approachusingSystem;classGFG{// Function to calculate the length// of the latus rectum of an ellipsestaticdoublelengthOfLatusRectum(doubleA,doubleB){// Length of major axisdoublemajor=2.0*A;// Length of minor axisdoubleminor=2.0*B;// Length of the latus rectumdoublelatus_rectum=(minor*minor)/major;returnlatus_rectum;}// Driver CodepublicstaticvoidMain(){// Given lengths of semi-major// and semi-minor axisdoubleA=3.0,B=2.0;// Function call to calculate length // of the latus rectum of a ellipseConsole.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 ellipsefunctionlengthOfLatusRectum(A,B){// Length of major axisvarmajor=2.0*A;// Length of minor axisvarminor=2.0*B;// Length of the latus rectumvarlatus_rectum=(minor*minor)/major;returnlatus_rectum;}// Driver code// Given lengths of semi-major// and semi-minor axisvarA=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 RectumdoublelatusRectum(doublea,doubleb){return(2*b*b)/a;}intmain(){// Example inputsdoublea1=3,b1=2;doublea2=6,b2=3;// Calculate the length of Latus Rectum for the first ellipsedoublel1=latusRectum(a1,b1);// Display the result with formatted stringstd::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 ellipsedoublel2=latusRectum(a2,b2);// Display the result with formatted stringstd::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;return0;}
Java
publicclassMain{// Function to calculate the length of Latus RectumstaticdoublelatusRectum(doublea,doubleb){return(2*b*b)/a;}publicstaticvoidmain(String[]args){// Example inputsdoublea1=3,b1=2;doublea2=6,b2=3;// Calculate the length of Latus Rectum for the first ellipsedoublel1=latusRectum(a1,b1);// Display the result with formatted stringSystem.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 ellipsedoublel2=latusRectum(a2,b2);// Display the result with formatted stringSystem.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
deflatus_rectum(a,b):return2*b**2/a# Example inputsa1,b1=3,2a2,b2=6,3# Calculate the length of Latus Rectum for the first ellipsel1=latus_rectum(a1,b1)# Display the result with formatted stringprint(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 ellipsel2=latus_rectum(a2,b2)# Display the result with formatted stringprint(f"The length of the Latus Rectum of the ellipse with a = {a2} and b = {b2} is {l2:.5f}")
C#
usingSystem;classProgram{// Function to calculate the length of Latus RectumstaticdoubleLatusRectum(doublea,doubleb){return(2*b*b)/a;}staticvoidMain(){// Example inputsdoublea1=3,b1=2;doublea2=6,b2=3;// Calculate the length of Latus Rectum for the first ellipsedoublel1=LatusRectum(a1,b1);// Display the result with formatted stringConsole.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 ellipsedoublel2=LatusRectum(a2,b2);// Display the result with formatted stringConsole.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 RectumfunctionlatusRectum(a,b){return(2*b*b)/a;}// Example inputsleta1=3,b1=2;leta2=6,b2=3;// Calculate the length of Latus Rectum for the first ellipseletl1=latusRectum(a1,b1);// Display the result with formatted stringconsole.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 ellipseletl2=latusRectum(a2,b2);// Display the result with formatted stringconsole.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