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.66666Input: 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++
// C++ program for the above approach#include <iostream>using namespace std;// Function to calculate the length// of the latus rectum of an ellipsedouble 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 Codeint 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 approachimport java.util.*;class GFG{// Function to calculate the length// of the latus rectum of an ellipsestatic 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 codepublic 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 ellipsedef 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 Codeif __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 approachusing 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 ellipsefunction 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 axisvar A = 3.0, B = 2.0;document.write(lengthOfLatusRectum(A, B));// This code is contributed by Ankita saini </script> |
2.66667
Time Complexity: O(1)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.



