Program to find Length of Latus Rectum of an Ellipse
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:
Ellipse
- Length of major axis is 2A.
- Length of minor axis is 2B.
- Therefore, the length of the latus rectum is:
Â
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++
#include <iostream>
using namespace std;
double lengthOfLatusRectum( double A,
double B)
{
double major = 2.0 * A;
double minor = 2.0 * B;
double latus_rectum = (minor*minor)/major;
return latus_rectum;
}
int main()
{
double A = 3.0, B = 2.0;
cout << lengthOfLatusRectum(A, B);
return 0;
}
|
Java
import java.util.*;
class GFG{
static double lengthOfLatusRectum( double A,
double B)
{
double major = 2.0 * A;
double minor = 2.0 * B;
double latus_rectum = (minor * minor) / major;
return latus_rectum;
}
public static void main(String[] args)
{
double A = 3.0 , B = 2.0 ;
System.out.print(lengthOfLatusRectum(A, B));
}
}
|
Python3
def lengthOfLatusRectum(A, B):
major = 2.0 * A
minor = 2.0 * B
latus_rectum = (minor * minor) / major
return latus_rectum
if __name__ = = "__main__" :
A = 3.0
B = 2.0
print ( '%.5f' % lengthOfLatusRectum(A, B))
|
C#
using System;
class GFG
{
static double lengthOfLatusRectum( double A,
double B)
{
double major = 2.0 * A;
double minor = 2.0 * B;
double latus_rectum = (minor*minor)/major;
return latus_rectum;
}
public static void Main()
{
double A = 3.0, B = 2.0;
Console.WriteLine(lengthOfLatusRectum(A, B));
}
}
|
Javascript
<script>
function lengthOfLatusRectum(A, B)
{
var major = 2.0 * A;
var minor = 2.0 * B;
var latus_rectum = (minor * minor) / major;
return latus_rectum;
}
var A = 3.0, B = 2.0;
document.write(lengthOfLatusRectum(A, B));
</script>
|
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
double latusRectum( double a, double b) {
return (2 * b * b) / a;
}
int main() {
double a1 = 3, b1 = 2;
double a2 = 6, b2 = 3;
double l1 = latusRectum(a1, b1);
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;
double l2 = latusRectum(a2, b2);
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 {
static double latusRectum( double a, double b) {
return ( 2 * b * b) / a;
}
public static void main(String[] args) {
double a1 = 3 , b1 = 2 ;
double a2 = 6 , b2 = 3 ;
double l1 = latusRectum(a1, b1);
System.out.println( "The length of the Latus Rectum of the ellipse with a = " + a1 +
" and b = " + b1 + " is " + String.format( "%.5f" , l1));
double l2 = latusRectum(a2, b2);
System.out.println( "The length of the Latus Rectum of the ellipse with a = " + a2 +
" and b = " + b2 + " is " + String.format( "%.5f" , l2));
}
}
|
Python3
def latus_rectum(a, b):
return 2 * b * * 2 / a
a1, b1 = 3 , 2
a2, b2 = 6 , 3
l1 = latus_rectum(a1, b1)
print (f "The length of the Latus Rectum of the ellipse with a = {a1} and b = {b1} is {l1:.5f}" )
l2 = latus_rectum(a2, b2)
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
{
static double LatusRectum( double a, double b)
{
return (2 * b * b) / a;
}
static void Main()
{
double a1 = 3, b1 = 2;
double a2 = 6, b2 = 3;
double l1 = LatusRectum(a1, b1);
Console.WriteLine($ "The length of the Latus Rectum of the ellipse with a = {a1} and b = {b1} is {l1:F5}" );
double l2 = LatusRectum(a2, b2);
Console.WriteLine($ "The length of the Latus Rectum of the ellipse with a = {a2} and b = {b2} is {l2:F5}" );
}
}
|
Javascript
function latusRectum(a, b) {
return (2 * b * b) / a;
}
let a1 = 3, b1 = 2;
let a2 = 6, b2 = 3;
let l1 = latusRectum(a1, b1);
console.log(`The length of the Latus Rectum of the ellipse with a = ${a1} and b = ${b1} is ${l1.toFixed(5)}`);
let l2 = latusRectum(a2, b2);
console.log(`The length of the Latus Rectum of the ellipse with a = ${a2} and b = ${b2} is ${l2.toFixed(5)}`);
|
OutputThe 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)
Last Updated :
04 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...