Program to find Length of Latus Rectum of an Ellipse (original) (raw)
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 ****(x** 2 / A 2 ) + (y 2 / B 2 ) = 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 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_rectumDriver 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
`
**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 #include // 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 = <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>a</mi><mn>1</mn></mrow><mi>a</mi><mi>n</mi><mi>d</mi><mi>b</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">{a1} and b = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord"><span class="mord mathnormal">a</span><span class="mord">1</span></span><span class="mord mathnormal">an</span><span class="mord mathnormal">d</span><span class="mord mathnormal">b</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>{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 = <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>a</mi><mn>2</mn></mrow><mi>a</mi><mi>n</mi><mi>d</mi><mi>b</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">{a2} and b = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord"><span class="mord mathnormal">a</span><span class="mord">2</span></span><span class="mord mathnormal">an</span><span class="mord mathnormal">d</span><span class="mord mathnormal">b</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>{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)
