椭圆被描述为围绕两个焦点的平面上的曲线,使得到两个焦点的距离之和对于曲线上的每个点都是恒定的。椭圆有两种类型的轴——长轴和短轴。椭圆的最长弦是长轴。与长轴垂直的弦是短轴,它在中心处平分长轴。
给定椭圆的短轴和长轴的长度,任务是找到椭圆的周长。
例子:
Input: a = 3, b = 2
Output: 16.0109
Input: a = 9, b = 5
Output: 45.7191
椭圆的周长是:
Perimeter : 2π * sqrt( (a2 + b2) / 2 )
其中 a 和 b 分别是长半轴和短半轴。
下面是上述方法的实现:
C++
// C++ program to find perimeter of an Ellipse
#include
using namespace std;
// Function to find the perimeter of an Ellipse
void Perimeter(int a, int b)
{
float perimeter;
// Compute perimeter
perimeter = 2 * 3.14 * sqrt((a * a + b * b) / (2 * 1.0));
cout << perimeter;
}
// Driver code
int main()
{
int a = 3, b = 2;
// Function call
Perimeter(a, b);
return 0;
}
Java
// Java program to find perimeter of
// an Ellipse.
import java.lang.Math;
class GFG1{
// Function to find perimeter of an
// ellipse.
static void Perimeter( double a, double b)
{
double Perimeter;
// formula to find the Perimeter
// of an Ellipse.
Perimeter = (double)2 * 3.14 * Math.sqrt((a * a + b * b) / (2 * 1.0)) ;
// Display the result
System.out.println("Perimeter: " + Perimeter);
}
// Driver code
public static void main (String[] args)
{
double a = 3, b = 2;
Perimeter(a , b);
}
}
Python3
# Python3 program to find perimeter
# of an Ellipse
from math import sqrt
# Function to find the perimeter
# of an Ellipse
def Perimeter(a, b):
perimeter = 0
# Compute perimeter
perimeter = (2 * 3.14 *
sqrt((a * a + b * b) /
(2 * 1.0)));
print(perimeter)
# Driver code
a = 3
b = 2
# Function call
Perimeter(a, b)
# This code is contributed
# by Mohit Kumar
C#
// C# program to find perimeter of
// an Ellipse.
using System;
class GFG1
{
// Function to find perimeter of an
// ellipse.
static void Perimeter(double a, double b)
{
double Perimeter;
// formula to find the Perimeter
// of an Ellipse.
Perimeter = (double)2 * 3.14 *
Math.Sqrt((a * a + b * b) / (2 * 1.0));
// Display the result
Console.WriteLine("Perimeter: " + Perimeter);
}
// Driver code
public static void Main (String[] args)
{
double a = 3, b = 2;
Perimeter(a , b);
}
}
// This code is contributed by Princi Singh
Javascript
输出 :
16.010921272681344
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。